This article describes the life cycle of the activty, and the impact of Launchmode on the life cycle, onnewintent,onsaveinstancestate,onrestoreinstancestate when the method calls
Activity life cycle Diagram
The following describes the life cycle of the activity
1, start the activity, the system will call the OnCreate method, then call the OnStart method, and then call the Onresume method, when the activity is running, the user can see the activity corresponding interface, the interface can interact with the user.
2, when the activity is in the Onresume state, activity is not fully covered by other interfaces, such as pop-up dialog, and so on, the system will call OnPause method, pause the activity, the focus to the pop-up dialog, At this point the user can see part of the activity, a part of the dialog occlusion, activity can not interact with the user, because the focus on the dialog.
3, when the activity in 2 state, at this time cancel dialog, let activty all visible, at this time the system will call Onresume, let Activity regain focus, back to the running state.
4, when the activity is in the OnPause state, if the activity is completely obscured, or back to the background. For example, the system calls the OnStop method by ejecting a full-screen interface to cover the activity or by pressing the power button to extinguish the screen.
5, when the activity is in the Onresume state, if the activity is completely obscured or back to the background, such as pressing the home button, press the power button, or pop up other full-screen activity, then the system will call OnPause, and then call OnStop.
6, when activity in OnStop state, that is, in the background state, restart the activity, For example, starting from a multi-task, booting from the desktop, the system will first call Onrestart, then call OnStart, and then call Onresume, let the activity back to the running state
7, when the activity is in the Onresume state, the user ends the current activity, such as clicking the Return key, or calling the finish method, then the system calls OnPause first, then calls OnStop, and then calls OnDestroy.
8, if the activity is in the background, that is, the OnStop state, then when the activty is reclaimed, is forced to end, will not execute ondestory, the activty will be stopped directly, click Start Again, will re-execute the corresponding life cycle method.
9, when the activty is in the Onresume state, the activity is re-state, such as rotating the screen, call the recreate method, the system will call OnPause first, then call OnStop, and then call OnDestroy, the activty is closed, The system restarts the activty immediately, calls OnCreate, calls OnStart, and then calls Onresume.
10, when the activty in the OnStop state, that is, in the background, this time to switch system settings, such as font, language and so on. At this point activty does not perform a life cycle method, and when Activty is started again, OnDestroy is called first, then Activty is called OnCreate First, then OnStart, and then Onresume is called.
Activity's startup mode is Android:launchmode
The activity has four startup modes, standard,singletop,singletask,singleinstance for each. This property is set in Androidmanifest.xml.
<activity android:name= ". Mainactivity " android:label=" @string/app_name " android:launchmode=" singleinstance "> < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity>
1,standard
This mode is the default mode, that is, if the activity tag is not set Android:lanuchmode, then Launchmode is standard. In this mode, a new instance object is created each time the activity is started, allowing different instances of the same activity to exist simultaneously.
Such as:
The first time you start Activty1, a task is created, the Activty1 is put into a task, and if you start Activty1 again in Activty1, a Activty1 instance is recreated to add to the task. If you start Activty1 again, then re-create an instance of Activity1 and add it to the task again, there are three Activty1 instance objects in the task, click Back, and the three instances of Activty1 will be one out of the stack.
2,singletop
The startup mode, the same activity can have multiple different instance objects. When activity is started, if the activity at the top of the stack is an instance of the activity, then an instance of the activity is not recreated, but the instance object of the activity at the top of the stack is reused. The activity instance at the top of the stack onnewintent the method. If the activity instance at the top of the stack is not an instance object of the activity, the instance object of the activity is recreated and the instance object is added to the task.
Such as:
Starts Activity1, a task is created and Activity1 is added to the task. If you start Activity1 again, because the activity instance at the top of the task is an instance of Activity1, the instance of Activity1 is not recreated, but the Activty1 at the top of the task is reused. If Activity2 is started at this point, because the instance at the top of the task is an instance of Activity1 and there are no instances of Activity2 in the task, then an instance of Activity2 is recreated and the Activity2 instance is added to the task. The activity instance at the top of the task is Activity2. If you start Activity2 again, because the activity instance at the top of the stack is an instance of Activity2, the instance of Activity2 is not recreated, but instead the instance of Activity2 at the top of the task is reused. The Onnewintent method of the instance is called. If you start Activity1 at this point, because the activity instance at the top of the stack is an instance of Activity2, a Activity1 instance is recreated to add the instance to the task. There are two instances of Activity1 in the task, and there is one instance of Activity2.
3.singleTask
There is only one instance of activity in this mode, and when the activity is started within the same program, if there is no instance object for the activity in the task, a new activity object is created and the object is added to the task. If there is an instance of the activity in the task, then all instances on that activity instance are destroyed, and the activity instance becomes an instance of the top of the stack, and the Onnewintent method of that instance is called.
Such as:
Starting Activity1, an instance of Activity1 is created and added to the task, starting Activity2, creating an instance of Activty2, adding to the task, starting Activty3, Creates an instance of Activity3 and adds the instance to the task, at which time there are three activity instances in the task. The Activity3 instance at the top of the task, at this point, if Activity2 is started, all instances (Activity3) above the Activity2 instance will be destroyed because there are instances of Activity2 in the task. At this point Activity2 becomes the instance at the top of the task. If Activity2 is not started again, Activity1 is started, because there are Activity1 instances in the task, the instance above the Activity1 instance is destroyed, and Activity1 becomes the instance at the top of the task. And Onnewintent will be called.
4,singleinstance
In this mode, there is only one instance of the activity, and it runs in a separate task, and there are no other instances of activity in the task.
Such as:
Start Activity1, create an instance of Activity1, and add to the task, at which point the lanuchmode of Activity2,activity2 is singleinstance, a new task is created, Place an instance of Activity2 in a new task. At this point the lanuchmode that starts Activity3,activity3 is non-singleinstance, then the instance of Activity3 is added to the same task as Activity1. If the Activity3 Lanuchmode is also singleinstance, a new task is recreated and the Activity3 instance is placed in the new task. Such as:
Impact of startup mode on activity life cycle
It can be understood that the startup mode does not affect the activity's normal life cycle, usually only involves the invocation of the Onnewintent method, does not set Android:lanuchmode or Lanuchmode as standard, Then onnewintent certainly will not be called, the other three modes, may call the Onnewintent method, not necessarily be called. Singletop mode, as long as the top of the stack is the activity instance to be started, then onnewintent will be called. Singletask,singleinstance, if there is always a corresponding activity instance in the same task, then Onnewintent will be called.
Onsaveinstancestate and the invocation of the Onrestoreinstancestate method
Onsaveinstancestate is mainly used to save some temporary data, when the activity resumes, the corresponding data can be restored, onrestoreinstancestate when the activity is re-created when the call, you can from the method to recover some previously saved data.
Onsaveinstancestate is usually called when the activity is "interrupted", such as being overwritten, including full or partial coverage, or by pressing home back to the background, or by killing the screen. Another scenario is when the activity is recreated and invoked, such as when the user rotates the screen at the current activity, or the user calls the recreate method. Click the Back button the method will not be called. The order of Onsaveinstancestate is called after OnPause, before OnStop.
Onrestoreinstancestate is typically called when activty is recreated, such as a user switching between a screen and a portrait. The user hangs the activty, switches the system settings, and then initiates the activity.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The activity life cycle and the impact of Launchmode on the life cycle