UNDERSTANDING ACTIVITIES

Similar documents
LECTURE NOTES OF APPLICATION ACTIVITIES

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Lab 1: Getting Started With Android Programming

Android Activities. Akhilesh Tyagi

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Activities. Repo:

Overview of Activities

Vienos veiklos būsena. Theory

Embedded Systems Programming - PA8001

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Real-Time Embedded Systems

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

Lifecycle Callbacks and Intents

LifeStreet Media Android Publisher SDK Integration Guide

States of Activities. Active Pause Stop Inactive

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

FRAGMENTS.

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Android framework overview Activity lifecycle and states

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

8Messaging SMS MESSAGING.

Android Fundamentals - Part 1

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Computer Science E-76 Building Mobile Applications

Programming with Android: Intents. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

CMSC436: Fall 2013 Week 3 Lab

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

ABSTRACT I. INTRODUCTION. E. Susmitha, M. Himabindu M.Tech Academic Assistant, IIIT Rk Valley, Rgukt, Andhra Pradesh, India

Introduction to Android development

Introduction To Android

Android Ecosystem and. Revised v4presenter. What s New

Minds-on: Android. Session 2

Activities and Fragments

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

UI Fragment.

Configuring the Android Manifest File

Programming Mobile Applications with Android Lab2

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

Mobile Application Development MyRent Settings

Android Services. Victor Matos Cleveland State University. Services

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Android

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Android Application Development. By : Shibaji Debnath

Xin Pan. CSCI Fall

Android Application Development

Lifecycle-Aware Components Live Data ViewModel Room Library

COLLEGE OF ENGINEERING, NASHIK-4

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Gauthier Picard. Ecole Nationale Supérieure des Mines

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

Understanding Application

INTRODUCTION TO ANDROID

Introduction to Android

ANDROID APP DEVELOPMENT

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android Programmierung leichtgemacht. Lars Vogel

12 Publishing Android Applications

Android Tutorial: Part 3

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

Applications. Marco Ronchetti Università degli Studi di Trento

Multiple Activities. Many apps have multiple activities

EMBEDDED SYSTEMS PROGRAMMING Android NDK

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (Part 1-2) Hello-Goodbye App Tutorial

Intents & Intent Filters. codeandroid.org

Application Fundamentals

Android Beginners Workshop

Android Workshop: Model View Controller ( MVC):

Using Eclipse for Android Development

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

COPYRIGHTED MATERIAL. 1Getting Started with Android Programming

Mobile User Interfaces

Our First Android Application

BCA 6. Question Bank

Mobile Application Development - Android

Android Programming Lecture 2 9/7/2011

ECOM 5341 Mobile Computing(Android) Eng.Ruba A. Salamah

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

By Ryan Hodson. Foreword by Daniel Jebaraj

Fragments and the Maps API

COSC 3P97 Mobile Computing

Security model. Marco Ronchetti Università degli Studi di Trento

Mobila applikationer och trådlösa nät, HI1033, HT2012

Google Maps Troubleshooting

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

Solving an Android Threading Problem

Application s Life Cycle

Transcription:

Activities

Activity is a window that contains the user interface of your application. An Android activity is both a unit of user interaction - typically filling the whole screen of an Android mobile device - and a unit of execution. An application can have zero or more activities. Main purpose of an activity is to interact with the user.

When you make an interactive Android program, you start by sub classing the AppCompatActivity class. Activities provide the reusable, interchangeable parts of the flow of UI components across Android applications. Activity s life cycle To create an activity, you create a Java class that extends the Activity base class.

public class TestActivity extends AppCompatActivity { /* Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main);

Activity class loads its UI component using the XML(main.xml) file defined in your res/layout folder. setcontentview(r.layout.activity_main); Every activity you have in your application must be declared in your AndroidManifest.xml file.

<?xml version= 1.0 encoding= utf-8?> <manifest xmlns:android= http://schemas.android.com/apk/res/android package= net.learn2develop.activity101 android:versioncode= 1 android:versionname= 1.0 > <uses-sdk android:minsdkversion= 14 /> <application android:icon= @drawable/ic_launcher android:label= @string/app_name > <activity android:label= @string/app_name android:name=. TestActivity > <intent-filter > <action android:name= android.intent.action.main /> <category android:name= android.intent.category.launcher /> </intent-filter> </activity> </application> </manifest>

Intent Class How one activity invokes another, and pass information about what the user wants to do? The unit of communication is the Intent class. An Intent represents an abstract description of a function that one activity requires another activity to perform. The code that implements one activity does not directly call methods in the code that implements another activity.

Intent Class

The Activity base class defines a series of events that govern the life cycle of an activity. oncreate() Called when the activity is first created onstart() Called when the activity becomes visible onresume() Called when the activity starts interacting with the user

onpause() Called when the current activity is being paused and the previous activity is being resumed onstop() Called when the activity is no longer visible ondestroy() Called before the activity is destroyed by the system (either manually or by the system to conserve memory) onrestart() Called when the activity has been stopped and is restarting again

import android.app.activity; import android.os.bundle; import android.util.log; public class TestActivity extends Activity { String tag = Lifecycle ; /** Called when the activity is first created. */ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Log.d(tag, In the oncreate() event ); public void onstart(){ super.onstart(); Log.d(tag, In the onstart() event );

public void onrestart() { super.onrestart(); Log.d(tag, In the onrestart() event ); public void onpause() { super.onpause(); Log.d(tag, In the onpause() event ); public void onstop() { super.onstop(); Log.d(tag, In the onstop() event ); public void ondestroy() { super.ondestroy(); Log.d(tag, In the ondestroy() event );

When the activity is first loaded, you should see something very similar to the following in the LogCat window (click the Debug perspective) 11-16 06:25:59.396: D/Lifecycle(559): In the oncreate() event 11-16 06:25:59.396: D/Lifecycle(559): In the onstart() event 11-16 06:25:59.396: D/Lifecycle(559): In the onresume() event If you click the Back button on the Android emulator, the following is printed: 11-16 06:29:26.665: D/Lifecycle(559): In the onpause() event 11-16 06:29:28.465: D/Lifecycle(559): In the onstop() event 11-16 06:29:28.465: D/Lifecycle(559): In the ondestroy() event

Click the Home button and hold it there. Click the Activities icon 11-16 06:31:08.905: D/Lifecycle(559): In the oncreate() event 11-16 06:31:08.905: D/Lifecycle(559): In the onstart() event 11-16 06:31:08.925: D/Lifecycle(559): In the onresume() event Click the Phone button on the Android emulator so that the activity is pushed to the background. 11-16 06:32:00.585: D/Lifecycle(559): In the onpause() event 11-16 06:32:05.015: D/Lifecycle(559): In the onstop() event Notice that the ondestroy() event is not called, indicating that the activity is still in memory. Exit the phone dialer by clicking the Back button. The activity is now visible again. 11-16 06:32:50.515: D/Lifecycle(559): In the onrestart() event 11-16 06:32:50.515: D/Lifecycle(559): In the onstart() event 11-16 06:32:50.515: D/Lifecycle(559): In the onresume() event

Important info Use the oncreate() method to create and instantiate the objects that you will be using in your application. Use the onresume() method to start any services or code that needs to run while your activity is in the foreground. Use the onpause() method to stop any services or code that does not need to run when your activity is not in the foreground. Destroy everything that is not used anymore, such as background processes, in ondestroy()

Activities and AndroidManifest.xml An Android application can be composed of multiple Activities Each activity should be declared in the file: AndroidManifest.xml Add a child element to the <application> tag: <application> <activity android:name=".myactivity" /> <activity android:name=.secondactivity" /> </application>

Activities and AndroidManifest.xml Each activity has its own Java class and a layout file. public class FirstActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_first); public class SecondActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_two);

THE END