First, a brief introduction
When the user starts, pauses, exits, and re-enters an app, the activity instance in the app transitions between different states of its life cycle. For example, when you start an activity for the first time, the activity comes to the front of the system to get user focus. In this process, the Android system invokes a series of life-cycle methods. If the user launches or switches to another app, the system calls another series of methods.
The life cycle invocation method describes what activities the activity performs when the user leaves or re-enters the activity.
Next we'll explain how each activity has a life-cycle approach and how to use them to accomplish the functionality the user needs, and not consume too much system resources when you don't need them.
Let's start with the official state of activity diagram. A box indicates the state that the activity may be in, and the arrow represents the function that the activity invokes when it is transformed in different states.
Removal is not started and destroyed, and activity can occur in 5 states at run time. Created,started,resumed,paused,stopped, respectively. But free three states are stable, and activity stays in these three states for a period of time. Each of them is:
Resumed in this state, activity is displayed on the screen and interacts with the user normally. The status of the normal operation.
Paused in this state, the activity is partially obscured by another activity, and the other activity is translucent or does not cover the entire screen. The activity is in a paused state and cannot interact with the user and cannot execute any code.
Stopped in this state, the activity is completely obscured and invisible to the user. When activity is in the stopped state, activity instances and state information exist, but Boone executes any code.
The other two states (created,started) can only exist for a very short time, and the system will quickly invoke the next life cycle method when the activity enters the next state, such as after the system calls OnCreate (), and quickly continues to call OnStart (), Then continue calling Onresume ().
Second, start and stop
After the user clicks on an app icon, the systemAndroidManifest.xml中寻找声明的主Activity,然后调用主Activity的onCreate()函数。通常onCreate()要被重载来定义Activity需要的变量,声明UI元素。
TextView Mtextview;//Member variable for text view in the layout@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set The user interface layout for this Activity//The layout file is defined in the project Res/layout/main_activity.xml fileSetcontentview (r.layout.main_activity); //Initialize member TextView So we can manipulate it laterMtextview =(TextView) Findviewbyid (r.id.text_message); //Make sure we ' re running on honeycomb or higher to use ActionBar APIs if(Build.VERSION.SDK_INT >=Build.version_codes. Honeycomb) {//for the main activity, make sure the app icon in the action bar//does not behave as a buttonActionBar ActionBar =Getactionbar (); Actionbar.sethomebuttonenabled (false); }}
OnCreate () After the call is complete, the system will soon continue to call OnStart () and Onresume (), and the activity will not stop in the created and started states. After the OnStart () call is complete, the activity is visible to the user, and then the system continues to call Onresume () and the activity stays in the resume state, which is the state of normal operation. Until something happens that changes this state.
The first call in the activity life cycle is oncreate (), and the last one is called ondestory (). This method is called to indicate that the activity instance is completely removed from the system.
Most applications do not require the Western line approach, because most local class references are automatically reclaimed by activity, and the rest of the Recycle cleanup needs to be done in OnPause () and OnStop (), but if there are background threads or other resources that could cause memory leaks in the activity, Need to be recycled in ondestory ().
Like what:
@Override Public void OnDestroy () { Super. OnDestroy (); // Always call the superclass // Stop method Tracing that the activity started during onCreate () android.os.Debug.stopMethodTracing ();}
Iii. Suspension and recovery
When activity is not completely obscured, it calls OnPause () into the paused state.
Developers need to reload OnPause () to stop the running behavior or save some information that needs to be persisted.
What developers need to implement in the OnPause () function is:
1. Stop animation waving other ongoing CPU-consuming behavior
2. Save some changes (variables that the user needs to save) such as a draft of the message.
3. Release system resources, such as broadcast receiver, sensor handles (such as GPS), or other power-hungry resources.
For example, if your application uses a camera, the OnPause () method is a good place to release the camera.
@Override public void OnPause () { super . OnPause ( ); // // Release the Camera because we don ' t need it when paused // and other activities might need to use it. if (Mcamera! = null = null ; }}
In general, OnPause () does not need to save user variables, onPause () need to save the variables that should be automatically saved, such as the message draft. Also, OnPause () avoids CPU-intensive work, such as writing to a database, because it slows down the visual process of transitioning to the next activity. CPU-intensive storage should be placed in OnStop ().
Onresume () is called when activity resumes from the paused state to the resumed state.
The activity calls the Onresume () function every time it comes to the front end, including when the application is first created, so the resources freed in OnPause () should be initialized in Onresume () and the code that the activity executes each time the user focuses is executed.
The following example of Onresume () is the inverse process of onpause (), which initializes the camera released in OnPause ()
@Override Public void Onresume () { Super. Onresume (); // Always call the superclass method First // Get The Camera instance as the activity achieves full user focus if NULL ) { // Local method to handle camera init }}
Translated from http://developer.android.com/training/basics/activity-lifecycle/index.html
[Android] manages the life cycle of activity