Activity life cycle:
An activity has three main states:
It is active or running when in the foreground of the screen (at the top of the current task stack). It is the activity that the corresponding user operates.
When it loses focus but is still visible to the user, it is paused. That is: There is another activity on top of it. The activity may be transparent, or the full screen cannot be completely obscured, so the suspended activity is still visible to the user. The suspended activity remains alive (it retains all state and member information and is connected to the window manager), but can still kill the activity when the system is in very low memory.
If it is completely covered by another activity, it is in a stopped state. It still retains all state and member information. However, it is not visible to the user, so its window will be hidden, and if memory is needed elsewhere, the system will often kill the activity.
If an activity is paused or stopped, the system can drive it out of memory by requiring it to end (call its finish () method) or directly kill its process. When it is visible to the user again, it can only be completely restarted and restored to its previous state.
When an activity transitions from this state to another State, it is notified by the following protected methods:
void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
You can overload all of these methods to do the right job when the state changes. All activity must implement onCreate () for initialization when the object is instantiated for the first time. In many cases I implement onpause () in the activity meeting to commit data changes or to stop interacting with the user.
Calling the parent class
The implementation of all activity lifecycle methods must first call the version of its parent class. For example:
protected void OnPause () {
Super.onpause ();
. . .
}
In total, these seven methods define the complete lifecycle of an activity. Implementing these methods can help you monitor three nested lifecycle loops:
An activity The complete lifecycle begins with the first call to OnCreate () until the OnDestroy () is invoked. The activity sets all the "global" states in OnCreate () to complete initialization and frees all system resources in OnDestroy (). For example, if an activity has a thread running in the background to download data from the network, it creates that thread with onCreate () and destroys that thread with OnDestroy ().
The visual life cycle of an activity begins with the OnStart () call until the corresponding onStop () call. During this time, users can see the activity on the screen, although it may not be in the foreground or is interacting with the user. In both of these methods, you can control which resources are used to display the activity to the user. For example, you can register a broadcastreceiver in OnStart () to monitor changes that affect your UI, and cancel the registration in OnStop (), when the user is unable to see what your program is displaying. The OnStart () and OnStop () methods can be called multiple times as the application is visible to the user.
The foreground life cycle of an activity is called from Onresume () to the corresponding OnPause () call. During this time, the activity is at the top of the foreground and interacts with the user. Activity often transitions between pauses and restores--for example, when a device goes into hibernation or a new activity starts, the OnPause () method is invoked. The Onresume () method is invoked when an activity obtains results or receives a new intent. Therefore, the code in these two methods should be lightweight.
The following diagram shows the above cyclic processes and the state changes experienced by the activity during this process. A shaded ellipse is a major state that an activity can experience. The rectangular box represents the callback method that you want to implement when the activity changes between states.