Open app:onCreate ()->onstart ()->onresume
back key:onPause ()->onstop ()->ondestory ( )
HOME key:onPause ()->onstop ()
Start again:Onrestart ()->onstart ()->onresume ( )
Three nested Loops
1. Overall life cycle:oncreate->-OnDestroy.
2. visual life cycle:onstop->-OnPause.
3. Focus life cycle:onpause-> onresume.
Four Stages
1. start Activity: Perform 3 life cycle methods in this phase:onCreate,onStart , and onresume.
2.Activity loses focus: If you enter another activity or application while the activity receives focus, the current activity loses focus. At this stage, the onPause and onStop methods are executed in turn.
3.Activity regain focus: If Activity regain Focus, 3 life cycle methods are executed sequentially:onrestart,OnStart and Onresume.
4. Close activity: When activity is closed, the system executes 3 life cycle methods:onPause,onStop and OnDestroy.
Seven methods
1.void onCreate (Bundle savedinstancestate)
WhenActivityexecuted when first loaded. When we start a new program, the main form of theonCreateThe event will be executed. IfActivityAfter being destroyed (OnDestroyback), then reload intoTaskWhen, itsonCreateThe event will also be re-executed. Note the parameters heresavedinstancestate(BundleA type is a set of key-value pairs that you can look at as. NetIn theDictionary) is a very useful design, due to the particular nature of the mobile applications mentioned earlier, aActivityis likely to be forced to swap into the background (swapping to the background means that the form is no longer visible to the user, but in fact it still exists in aTask, such as a newActivityPressed into the currentTaskThus "covering up" the currentActivity, or the user presses theHomeKeys back to the desktop, or other important events that cause newActivityAppears in the currentActivity, such as the caller interface), and if the user has not re-viewed the form for a period of time (Androidby Long PressHomeKey to select the most recently run6program, or the user directly clicks the program's Run icon again, if the form is locatedTaskAnd processes are not destroyed by the system, they do not have to be reloaded and are displayed directlyTaskTop ofActivity, which is called re-viewing a program's form), which is the same as theTaskAndProcessmay have been automatically destroyed by the system, if you look at the form again, you will have to re-executeonCreateThe event initialization form. And this time we might want the user to continue working on the state of the last time the form was opened, rather than starting from scratch. For example, the user in the text message when the sudden call, after the phone after the user to do some other things, such as saving the caller number to contact, and not immediately back to the text message editing interface, resulting in the text message editing interface is destroyed, when the user re-enter the text message program he may want to continue the last edit. In this case, we can override it.ActivityOfvoidonsaveinstancestate (bundleoutstate)event, by providingoutstateTo write some state or information that we need to save before the form is destroyed so that the form is re-executedonCreate, you will pass thesavedinstancestateBy passing in the previously saved information, we can selectively use this information to initialize the form instead of starting from scratch.
2.void OnStart () activity becomes called when the user is visible on the screen.
Executes after the onCreate event. Or, after the current form has been swapped to the background, the form has performed a onStop event before the user re-views the form, but the form and its process are not destroyed, and the onrestart event is executed when the user re-views the form again , then skips the onCreate event and executes the form's OnStart event directly.
3.void Onresume () activity is invoked when the activity begins to interact with the user (whether it is a startup or a restart, the method is always called).
Executes after the OnStart event. Or, after the current form is swapped to the background, the form has not been destroyed and the onStop event has not been executed (the form also continues to exist in the Task ) when the user re-views the form, skipping the form's onCreate and The OnStart event, which executes the Onresume event directly.
4.void onPause () activity is called when the CPU and other resources are paused or retracted, the method is used to save the active state, but also to protect the site, press stack bar!
The form is executed when it is swapped to the background.
5.void onStop () is called when activity is stopped and turned into an invisible phase and subsequent life-cycle events.
Executes after the onPause event. If the form is not re-viewed by the user for a period of time, the onStop event of the form is executed, or the user presses the back key directly to remove the form from the current Task and also executes the form's OnStop event.
6.void Onrestart () called when activity is restarted. The activity is still in the stack, not the start of a new activity.
After the onStop event executes, if the form and its process are not destroyed by the system, and the user re-views the form, the Onrestart event of the form is executed, and theOnrestart event skips the form's The onCreate event executes the OnStart event directly.
7.void OnDestroy () activity is called when it is completely removed from system memory, the method is called probably because someone calls the onfinish () method directly or the system decides to stop the activity to free up resources!
Executed when the Activity is destroyed. After the form's onStop event, theActivity is destroyed if the form is not viewed again.
1.onCreate: When the activity starts for the first time, the method is triggered, at which time the activity initialization can be completed.
The onCreate method has a parameter that can be empty ( null), or it can be a state information that was previously saved by calling the onsaveinstancestate() method.
2.onStart: The trigger of the method indicates that the activity will be displayed to the user.
3.onResume: Triggers the method when an activity interacts with the user.
4.onPause: This method is triggered when an activity that is running in the foreground is running in the background because other activities require the foreground to run. This is the time to persist the state of the activity, such as the database record being edited.
5.onStop: Triggers the method when an activity no longer needs to be displayed to the user. If the memory is tight, the system will end the activity directly without triggering the onStop method. So saving state information should be done at OnPause time, not onStop . Activities that are not running in the foreground will be stopped or the Linux management process can end these activities at any time in order to reserve enough storage space for the new activity. Therefore, it is important for developers to always keep this principle in mind when designing applications. In some cases, theonPause method may be the last method of activity triggering, so developers need to save the information at this time.
6.onRestart: Triggers This method when an activity in the stopped state needs to be presented to the user again.
7.onDestroy: Triggers This method when the activity is destroyed. As with the onStop method, if the memory is tight, the system will end the activity directly without triggering the method.
8.onSaveInstanceState: The system calls this method, allowing the activity to save the previous state, for example, in a string of strings where the cursor is located.
Typically, developers do not need to override this method, and in the default implementation, all state information for the user interface components involved in the AutoSave activity has been provided.
Android three ways to listen for events to add actions
The first is also the initial approach to contact, usually in the activity component of the OnCreate event directly defined, Direct action. This approach is defined once for each control and is usually inconvenient.
BUTTONBTN = (Button) Findviewbyid (R.id.mybutton);
Btn.setonclicklistener (Newview.onclicklistener () {
publicvoid OnClick (View v) {
//dosomething
}
});
The second is usually to implement its interface in the activity component, so that it is relatively convenient to share an interface with multiple external controls.
Publicclass My activity extends activity implements view.onclicklistener{
@Override
publicvoid OnClick (View v) {
switch (V.getid ()) {
}
}
}
The third, similar to the second, defines a specific instance, and the advantage is that it is clearer if multiple listening interfaces need to be implemented.
Classmyclicklistener Implements View.onclicklistener
@Override
publicvoid OnClick (View v) {
switch (V.getid ()) {
}
}
}
Android-Life Cycle Introduction