The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity...
Transcript of The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity...
![Page 1: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/1.jpg)
![Page 2: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/2.jpg)
![Page 3: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/3.jpg)
The activity class The task backstack The activity lifecycle Starting activities Handling configuration changes
![Page 4: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/4.jpg)
Provides a visual interface for user interaction
![Page 5: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/5.jpg)
Each activity typically supports one focused thing a user can do, such as
Viewing an email message Showing a login screen
![Page 6: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/6.jpg)
Applications often comprise several activities
![Page 7: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/7.jpg)
Android supports navigation in several ways: Tasks The task backstack Suspending & resuming activities
![Page 8: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/8.jpg)
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
![Page 9: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/9.jpg)
When an Activity is launched, it goes on top of the backstack When the Activity is destroyed, it is popped off the backstack
![Page 10: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/10.jpg)
Activity 3 Activity 2 Activity 1
Activity 1 Activity 2 Activity 1
![Page 11: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/11.jpg)
Activities are created, suspended, resumed & destroyed as necessary when an application executes
![Page 12: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/12.jpg)
Some of these actions depend on user behavior Some depend on Android
e.g., Android can kill activities when it needs their resources
![Page 13: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/13.jpg)
Resumed/Running - visible, user interacting Paused - visible, user not interacting, can be terminated* Stopped - not visible, can be terminated
![Page 14: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/14.jpg)
Android announces activity lifecycle state changes to activity by calling specific activity methods
![Page 15: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/15.jpg)
protected void onCreate (Bundle savedInstanceState) protected void onStart() protected void onResume() protected void onPause() protected void onRestart() protected void onStop() protected void onDestroy()
![Page 16: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/16.jpg)
![Page 17: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/17.jpg)
![Page 18: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/18.jpg)
Entire Lifetime
![Page 19: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/19.jpg)
Visible
![Page 20: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/20.jpg)
Visible & in Foreground
![Page 21: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/21.jpg)
![Page 22: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/22.jpg)
![Page 23: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/23.jpg)
![Page 24: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/24.jpg)
![Page 25: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/25.jpg)
Called when Activity is created Sets up Initial state
Call super.onCreate() Set the Activity’s content view Retain reference to UI views as necessary Configure views as necessary
![Page 26: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/26.jpg)
![Page 27: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/27.jpg)
Called if the Activity has been stopped and is about to be started again Typical actions
Special processing needed only after having been stopped
![Page 28: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/28.jpg)
Activity is about to become visible Typical actions
Start when visible-only behaviors Loading persistent application state
![Page 29: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/29.jpg)
Activity is visible and about to start interacting with user Typical actions
Start foreground-only behaviors
![Page 30: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/30.jpg)
Focus about to switch to another Activity Typical actions
Shutdown foreground-only behaviors Save persistent state
![Page 31: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/31.jpg)
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
![Page 32: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/32.jpg)
Activity is about to be destroyed Typical actions
Release Activity resources
Note: may not be called if Android kills your application
![Page 33: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/33.jpg)
![Page 34: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/34.jpg)
Create an Intent object specifying the Activity to start
![Page 35: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/35.jpg)
Pass newly created Intent to methods, such as: startActivity() startActivityForResult()
Invokes a Callback method when the called Activity finishes to return a result
![Page 36: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/36.jpg)
![Page 37: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/37.jpg)
Similar to MapLocation, but gets address from contacts database
![Page 38: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/38.jpg)
![Page 39: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/39.jpg)
![Page 40: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/40.jpg)
Started Activity can set its result by calling Activity.setResult()
public final void setResult (int resultCode) public final void setResult (int resultCode,
Intent data)
![Page 41: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/41.jpg)
resultCode (an int) RESULT_CANCELED RESULT_OK RESULT_FIRST_USER
Custom resultCodes can be added
![Page 42: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/42.jpg)
![Page 43: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/43.jpg)
Device configuration can change at runtime
Keyboard, orientation, locale, etc.
On configuration changes, Android usually kills the current Activity & then restarts it
![Page 44: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/44.jpg)
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
![Page 45: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/45.jpg)
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()
![Page 46: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/46.jpg)
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)
![Page 47: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/47.jpg)
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”…>
![Page 48: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/48.jpg)
When configuration changes, Activity’ s onConfigurationChanged() method is called Passed a Configuration object specifying the new device configuration
![Page 49: The activity class The task backstack The activity lifecycle · 2013. 10. 22. · The activity lifecycle Starting activities Handling configuration changes . Provides a visual interface](https://reader034.fdocuments.in/reader034/viewer/2022051904/5ff5c3f5cfa6a41f3d00f82d/html5/thumbnails/49.jpg)
The Intent Class