activity class
DESCRIPTION
Activity Class Description for AndriodTRANSCRIPT
Romain Chiappinelli
The activity class The task backstack The activity lifecycle Starting activities Handling configuration changes
Provides a visual interface for user interaction
Each activity typically supports one focused thing a user can do, such as
Viewing an email message Showing a login screen
Applications often comprise several activities
Android supports navigation in several ways: Tasks The task backstack Suspending & resuming activities
A Task is a set of related Activities These related activities don’t have to be part of the same application Most tasks start at the home screen
See: http://developer.android.com/guide/topics/ fundamentals/tasks-and-back-stack.html
When an Activity is launched, it goes on top of the backstack When the Activity is destroyed, it is popped off the backstack
Activity 3 Activity 2 Activity 1
Activity 1 Activity 2 Activity 1
Activities are created, suspended, resumed & destroyed as necessary when an application executes
Some of these actions depend on user behavior Some depend on Android
e.g., Android can kill activities when it needs their resources
Resumed/Running - visible, user interacting Paused - visible, user not interacting, can be terminated* Stopped - not visible, can be terminated
Android announces activity lifecycle state changes to activity by calling specific activity methods
protected void onCreate (Bundle savedInstanceState) protected void onStart() protected void onResume() protected void onPause() protected void onRestart() protected void onStop() protected void onDestroy()
Entire Lifetime
Visible
Visible & in Foreground
Called when Activity is created Sets up Initial state
Call super.onCreate() Set the Activity’s content view Retain references to UI views as necessary Configure views as necessary
MapLocation
Called if the Activity has been stopped and is about to be started again Typical actions
Special processing needed only after having been stopped
Activity is about to become visible Typical actions
Start when visible-only behaviors Loading persistent application state
Activity is visible and about to start interacting with user Typical actions
Start foreground-only behaviors
Focus about to switch to another Activity Typical actions
Shutdown foreground-only behaviors Save persistent state
Activity is no longer visible to user may be restarted later
Typical actions Cache state
Note: may not be called if Android kills your application
Activity is about to be destroyed Typical actions
Release Activity resources
Note: may not be called if Android kills your application
MapLocation
MapLocation
Create an Intent object specifying the Activity to start
Pass newly created Intent to methods, such as: startActivity() startActivityForResult()
Invokes a Callback method when the called Activity finishes to return a result
MapLocation
Similar to MapLocation, but gets address from contacts database
MapLocationFromContacts
Started Activity can set its result by calling Activity.setResult()
public final void setResult (int resultCode) public final void setResult (int resultCode,
Intent data)
resultCode (an int) RESULT_CANCELED RESULT_OK RESULT_FIRST_USER
Custom resultCodes can be added
MapLocationFromContacts
MapLocationFromContacts
Keyboard, orientation, locale, etc.
Device configuration can change at runtime On configuration changes, Android usually kills the current Activity & then restarts it
Activity restarting should be fast If necessary you can:
Retain an Object containing important state information during a configuration change Manually handle the configuration change
Hard to recompute data can be cached to speed up handling of configuration changes Override onRetainNonConfigurationInstance() to build & return configuration Object
Will be called between onStop() and onDestroy()
Call getLastNonConfigurationInstance() during onCreate() to recover retained Object Note: These methods have been deprecated in favor of methods in the Fragment class (discussed in later classes)
Can prevent system from restarting Activity Declare the configuration changes your Activity handles in AndroidManifest.xml file, e.g., <activity android:name=".MyActivity“� android:configChanges=�
"orientation|screensize|keyboardHidden”…>
When configuration changes, Activity’ s onConfigurationChanged() method is called Passed a Configuration object specifying the new device configuration
The Intent Class