- All of the callback methods are hooks that you can override to do appropriate work when
the state of your activity changes.
- Implementations of lifecycle methods must always call the superclass implementation
before doing any work.
- onCreate() - Called when the activity is first created.
An activity should perform setup of "global" state (such as defining layout).
This method is passed a Bundle object containing the activity's previous state,
if that state was captured.
Always followed by onStart().
//----The activity is being created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
- onStart() - Called just before the activity becomes visible to the user.
Followed by onResume() if the activity comes to the foreground, or
onStop() if it becomes hidden.
//----The activity is about to become visible.
@Override
protected void onStart() {
super.onStart();
}
- onResume() - Called just before the activity starts interacting with the user.
Always followed by onPause(), normally after some user interaction.
//----The activity has become visible (it is now "resumed").
@Override
protected void onResume() {
super.onResume();
}
- onPause() - Called when the system is about to resume 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 the method returns.
Followed by onResume() if the activity returns back to the front, or by
onStop() if it becomes invisible to the user.
//----Another activity is taking focus (this activity is about to be "paused").
@Override
protected void onPause() {
super.onPause();
}
- onStop() - Called when the activity is no longer visible to the user, either
because it is being destroyed or because a resumed activity has come in front of it.
Followed by onRestart() if the activity is going to be resumed, or by
onDestroy() if the activity is going to be destroyed.
//----The activity is no longer visible (it is now "stopped")
@Override
protected void onStop() {
super.onStop();
}
- onRestart() - Called after the activity has been stopped, just prior to it
being started again.
Always followed by onStart().
//----The activity is about to start, after having stopped.
@Override
protected void onRestart() {
super.onRestart();
}
- 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
finish() 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 isFinishing() method.
All resources should be released by here.
//----The activity is about to be destroyed.
@Override
protected void onDestroy() {
super.onDestroy();
}
- The order of lifecycle callbacks is well defined, particularly when the activities are
in the same process and one is starting the other.
Here's the order of callbacks that occur when Activity A starts Activity B:
- Activity A's onPause() is called.
- Activity B's onCreate(), onStart(), and onResume() are
called.
- If Activity A is no longer visible its onStop() is called.