http://developer.android.com/guide/topics/fundamentals/activities.html
Managing the lifecycle of your activities by implementing callback methods is crucial to developing a strong and flexible application. The lifecycle of an activity is directly affected by its association with other activities, its task and back stack.
An activity can exist in essentially three states:
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.
Activity
object is retained in memory, it maintains all state and member information, but is
notattached 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.
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.
When an activity transitions into and out of the different states described above, it is notified through various callback methods. All of the callback methods are hooks that you can override to do appropriate work when the state of your activity changes. The following skeleton activity includes each of the fundamental lifecycle methods:
public class ExampleActivity extends Activity {
@Override
public voidonCreate
(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
@Override
protected voidonStart()
{
super.onStart();
// The activity is about to become visible.
}
@Override
protected voidonResume()
{
super.onResume();
// The activity has become visible (it is now "resumed").
}
@Override
protected voidonPause()
{
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").
}
@Override
protected voidonStop()
{
super.onStop();
// The activity is no longer visible (it is now "stopped")
}
@Override
protected voidonDestroy()
{
super.onDestroy();
// The activity is about to be destroyed.
}
}
Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above.
Taken together, these methods define the entire lifecycle of an activity. By implementing these methods, you can monitor three nested loops in the activity lifecycle:
onCreate()
and the call to onDestroy()
. Your activity should perform setup of "global" state (such as defining layout) in onCreate()
, and release all remaining resources in onDestroy()
. For example, if your activity has a thread running in the background to download data from the network, it might create that thread in onCreate()
and then stop the thread in onDestroy()
.The visible lifetime of an activity happens between the call to onStart()
and the call to onStop()
. During this time, the user can see the activity on-screen and interact with it. For example, onStop()
is called when a new activity starts and this one is no longer visible. Between these two methods, you can maintain resources that are needed to show the activity to the user. For example, you can register aBroadcastReceiver
in onStart()
to monitor changes that impact your UI, and unregister it in onStop()
when the user can no longer see what you are displaying. The system might call onStart()
and onStop()
multiple times during the entire lifetime of the activity, as the activity alternates between being visible and hidden to the user.
The foreground lifetime of an activity happens between the call to onResume()
and the call to onPause()
. During this time, the activity is in front of all other activities on screen and has user input focus. An activity can frequently transition in and out of the foreground—for example, onPause()
is called when the device goes to sleep or when a dialog appears. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.
Figure 1 illustrates these loops and the paths an activity might take between states. The rectangles represent the callback methods you can implement to perform operations when the activity transitions between states.
The same lifecycle callback methods are listed in table 1, which describes each of the callback methods in more detail and locates each one within the activity's overall lifecycle, including whether the system can kill the activity after the callback method completes.
Method | Description | Killable after? | Next | ||
---|---|---|---|---|---|
|
Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (see Saving Activity State, later). Always followed by |
No | onStart() |
||
|
Called after the activity has been stopped, just prior to it being started again. Always followed by |
No | onStart() |
||
|
Called just before the activity becomes visible to the user. Followed by |
No | onResume() or onStop() |
||
|
Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it. Always followed by |
No | onPause() |
||
|
Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns. Followed either by |
Yes | onResume() or onStop() |
||
|
Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it. Followed either by |
Yes | onRestart() or onDestroy() |
||
|
Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the method. |
Yes | nothing |
The column labeled "Killable after?" indicates whether or not the system can kill the process hosting the activity at any time after the method returns, without executing another line of the activity's code. Three methods are marked "yes": (onPause()
, onStop()
, and onDestroy()
). Because onPause()
is the first of the three, once the activity is created, onPause()
is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop()
and onDestroy()
might not be called. Therefore, you should use onPause()
to write crucial persistent data (such as user edits) to storage. However, you should be selective about what information must be retained during onPause()
, because any blocking procedures in this method block the transition to the next activity and slow the user experience.
Methods that are marked "No" in the Killable column protect the process hosting the activity from being killed from the moment they are called. Thus, an activity is killable from the time onPause()
returns to the timeonResume()
is called. It will not again be killable untilonPause()
is again called and returns.
Note: An activity that's not technically "killable" by this definition in table 1 might still be killed by the system—but that would happen only in extreme circumstances when there is no other recourse. When an activity might be killed is discussed more in the Processes and Threading document.