1. Life cycle of activity
The activity life cycle as shown in the normal case (from Android Developer):
When the resource-related system configuration changes (such as the device screen orientation changes, keyboard visibility changes), the activity will be destroyed and rebuilt. When certain system configuration changes are made, the system is re-loaded with the latest configuration for the resources that are appropriate for the current configuration, which are resource-related system configurations. The Onsaveinstance () method is called before destroying the activity and the current state information is saved, which is called before OnStop (), but is not deterministic with the timing relationship of OnPause (). When the activity is recreated, the Onrestoreinstancestate () method is called to restore the previously saved state information, and the specific invocation time is after OnStart ().
The Onsaveinstancestate () method is only called when the destroyed activity is then re-created, mainly in the following two cases:
One is the resource-related system configuration changes we mentioned above;
Another situation is that the low-priority activity is killed by insufficient system memory, in which case the system calls the Onsaveinstancestate () method when the activity is destroyed, and then when the killed activity restarts, The Onrestoreinstancestate () method is recalled.
2. Acitivty Four startup modes
Before we introduce the activity, let's briefly introduce the concept of the task stack (back stack). Assuming we launch an app for the first time from the desktop, the system opens a new task stack and adds the app's mainactivity (set as Activity1) to the task stack, and if we then open Activity 2 in the new app, activity 3, Activity 2 and Activity 3 will also be pressed into the task stack in turn. So now the stack top of the task stack is activity 3, and the activity we interact with is always the activity at the top of the current task stack. Now that we have clicked the back key, Activity 3 will be popped up and the activity at the top of the current stack becomes activity 2, and we start interacting with activity 2.
The above procedure can be used to describe:
(1) Standard: Normal mode (default)
Activity started in standard mode is placed in the task stack (back stack) where the activity that started it is located. When we call the Context.startactivity method to start the activity of the standard mode, we get an error because ApplicationContext does not have a task stack associated with it, and the solution is to specify the activity to be started Flag_ Activity_new_task the tag bit, the new key is a task stack, and the activity that was just started is put into it, in which case the activity is actually started in Singletask mode. When you start activity in standard mode, multiple instances of activity are created if you start multiple times.
(2) Singletop: Stack top multiplexing mode
If the activity to be started is at the top of the stack, it is reused and no new instance will be used. The Onnewintent method of the activity to be started is called.
If the activity to be started is not at the top of the stack, an instance of it will still be created.
(3) Singletask: In-stack reuse mode
As long as the activity already exists in the stack it wants to use, it will reuse the activity instance that already exists in the stack without creating a new instance. By specifying the Taskaffinity property for activity, you specify the stack it wants to be on, and the default is the app package name. The Taskaffinity property is used primarily with Singletask startup mode or Allowtaskreparenting attribute pairing. If the activity is in the stack it wants to be in, the Onnewintent method to start the activity is called.
When we start an activity in a singletask mode, the system first looks for the stack to which the activity is to be started, and if so, whether there is an instance of it in that stack, and if so, it adjusts the instance to the top of the stack and calls its Onnewintent method. If the instance does not exist, create an activity instance and put it in the stack. If you can't find the stack you want, create a new stack, and then set up an instance of the activity to start and put it in.
Singletask comes with cleartop effect: if the activity to be started is in the stack it wants to be in (but not at the top of the stack), the system will stack all the activity above it, allowing the instance of the activity to be started to "ascend" to the top of the stack.
(4) SingleInstance: Single instance mode
This is an enhanced singletask model. In addition to having all the features of Singletask, it reinforces the point that there is only one activity in the task stack where activity with this pattern exists. The system will always create a new task stack for activity started in singleinstance mode, and then create an instance of it to put in.
Finally, we introduce the Allowtaskreparenting property: When app a launches an activity in app B, if the Allowtaskreparenting property of the activity being started is true, then when App B is started, This activity is transferred directly from the task stack of application A to the task stack of application B. Because actually the Taskaffinity property of activity in App B is the package name of app B, it would have been a task stack for B, but app B was not started at this point, so it was temporarily in A's task stack. Once we set the Allowtaskreparenting property of the activity to true, when the time is ripe (APP B is started), the activity goes where it belongs.
3. References
(1) Android Developer
(2) "Android Development Art exploration"
10 minutes to Master activity life cycle and startup mode