Created Wednesday 28 August 2013
- http://developer.android.com/guide/topics/fundamentals/activities.html
- http://developer.android.com/training/basics/activity-lifecycle/starting.html
- An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails.
- Activities are independent of each other.
- Other applications can start an activity. For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.
Activity States
+----------+ onResume()-->| Resumed |--->onPause()--o ^ | (visible)| | | +-----+----+ | +----+-----+ ^ +--------+----------+ onStart()-->| Started | onResume()<---| Paused | ^ | (visible)| |(partially visible)| +----+----+ +-----+----+ +--------+----------+ +---->| Created | ^ | | +---------+ | onStop() onCreate() | | | +------+--+ onRestart()<------------------| Stopped |----->onDestroy() +---------+
An activity can exist in essentially three states:
- Resumed (aka Running) - The activity is in the foreground of the screen and has user focus.
- Paused - Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
- Stopped (aka Background) - The activity is completely obscured by another activity. A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
- The other states (Created and Started) are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method. That is, after the system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume().
If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.
Note: In extreme cases, when the system is super low on memory, onDestroy() may never be called! However, onStop() is always called.
Pausing an Activity
- Stop actions that consume CPU.
- Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
- Release system resources that may affect battery life while your activity is paused and the user does not need them.
- Don't write to the database in onPause().
Note: onPause() is designed to be lightweight! Do heavyweight housekeeping like database access in onStop()
Stopping an Activity
Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources.
Recreating an Activity
When data to save is simple:
- Save state to the bundle in onSaveInstanceState(Bundle)
- Restore state from the saved bundle in onRestoreInstanceState(Bundle)
Disadvantage is that a Bundle stores objects with Parcelable (so it can be expensive). You may also need to reinstantiate expensive objects such as network connections. This can make rotating the phone seem sluggish. An alternative:
- Override the onRetainNonConfigurationInstance() method to return the object you would like to retain.
- When your activity is created again, call getLastNonConfigurationInstance() to recover your object.
Note: watch out for memory leaks! Don't store the Activity or anything associated with its Context.
No backlinks to this page. comments powered by Disqus