Explain in detail the four launchmodes of activity in Android Development

The activity stack is mainly used to manage the switching of activities. When using intent to jump to a target activity, it needs to be loaded according to the loading mode of the target activity.

Activity has the following four launchmodes:

1. Standard: by default, a new instance is created every time you jump to the target activity using intent. The disadvantage is that each time you enter, you need to create a new instance and execute the oncreate method.

2. Singletop: if the target activity to jump is just at the top of the task (it means that it is definitely not in the target task at present. For example, if I am on the wechat home page and want to jump to the home page of innoxyz application with intent, then join the innoxyz home page, which is just at the top of the innoxyz task, and jump directly instead of creating an instance), then jump directly to the past without creating a new one. (for example, at present, a wechat home page push is received on the home screen, and the top of the activity stack in the wechat task in the task stack is just the wechat home page, so click push to directly enter the instance without creating a new instance)

3. Singletask: this instance will be generated in a new task. It will be used every time it is called in the future. No new instance will be generated.

4. Singleinstance: it is the only activity on its stack, and it will be reused every time.

Launchmode plays an important role in the jump process of multiple activities. It can decide whether to generate new activity instances, reuse existing activity instances, and share a task with other activity instances. Here is a brief introduction to the concept of task. Task is an object with stack structure. A task can manage multiple activities, start an application, and create a corresponding task.

We can configure the Android: launchmode attribute of < activity > in androidmanifest.xml as one of the above four. Let's introduce the four lanchmodes one by one in combination with examples: 1. Standard standard mode is the default startup mode. You don't need to configure the Android: launchmode attribute for < activity >. Of course, you can also specify the value as standard. We will create an activity named firstactivity to demonstrate the standard startup mode. The firstactivity code is as follows:

Standard mode is the default startup mode. You don't need to configure the Android: launchmode attribute for < activity >. Of course, you can also specify the value as standard. We will create an activity named firstactivity to demonstrate the standard startup mode. The firstactivity code is as follows:

The textview in the firstactivity interface is used to display the serial number of the current activity instance, and the button is used to jump to the next firstactivity interface. Then we click the button several times in succession, and the following phenomenon will appear:

We notice that all instances are firstactivity, but the serial numbers are different, and we need to press the back button twice in a row to return to the first fristadtivity. The principle of standard mode is shown in the following figure:

As shown in the figure, the system will generate a new firstactivity instance in the task every time we jump and put it on the top of the stack structure. When we press the back key, we can see the original firstactivity instance. This is the standard startup mode. No matter whether there is an existing instance or not, a new instance is generated.

2. Singletop based on the above, we specify the attribute Android: launchmode = "singletop" for < activity >, and the system will process the jump behavior according to the singletop startup mode. If we repeat the above actions, the following phenomena will appear:

We see that this result is different from standard. The three serial numbers are the same, that is, the same firstactivity instance is used; If you press the back key, the program will exit immediately, indicating that there is only one activity instance in the current stack structure. The principle of singletop mode is shown in the following figure:

As shown in the figure above, when jumping, the system will first look for whether a firstactivity instance is at the top of the stack in the stack structure. If so, it will not generate a new instance, but will be used directly. Maybe my friends will have questions. I only see that there is only one activity in the stack. What if there are multiple activities? What if they are not at the top of the stack? Let's use an example to confirm your questions. Let's create another activity named secondactivity, as follows:

Then change the previous firstactivity jump code to:

Yes, firstactivity will jump to secondactivity, and secondactivity will jump to firstactivity. The demonstration results are as follows:

We can see that the serial numbers of the two firstactivities are different, which proves that a new firstactivity instance is generated when jumping from secondactivity to firstactivity. The schematic diagram is as follows:

We can see that when jumping from secondactivity to firstactivity, the system finds that there is an instance of firstactivity, but it is not at the top of the stack, so it regenerates an instance. This is the singletop startup mode. If a corresponding activity instance is found at the top of the stack, it will be reused and no new instance will be generated.

3. Singletask based on the above, we modify the property of firstactivity Android: launchmode = "singletask". The results of the demonstration are as follows:

We note that in the above process, the serial number of firstactivity remains unchanged, but the serial number of secondactivity is not unique. This means that when jumping from secondactivity to firstactivity, no new instance is generated, but when jumping from firstactivity to secondactivity, a new instance is generated. The schematic diagram of singletask mode is shown in the following figure:

The lower part of the figure is the result of the stack structure change after the second activity jumps to the firstactivity. We notice that the second activity disappears. Yes, in the jump process, the system finds that there are existing firstactivity instances, so no new instances are generated. Instead, all the activity instances above the firstactivity are out of the stack and the firstactivity is changed into a stack top object, Display to the front of the screen. Friends may have questions. If you set secondactivity to singletask mode, can secondactivity instances be unique? In our example, it is impossible because every time we jump from secondactivity to firstactivity, the secondactivity instance is forced out of the stack. The next time we jump from firstactivity to secondactivity, we can't find the existing secondactivity instance, so we must generate a new instance. However, if we have a thirdactivity and let the secondactivity and thirdactivity jump to each other, the secondactivity instance can be guaranteed to be unique. This is the singletask mode. If a corresponding activity instance is found, all other activity instances above this activity instance will be out of the stack, making this activity instance the top object of the stack and displayed in front of the screen.

4. The startup mode of singleinstance is special because it will enable a new stack structure, place acitvity in the new stack structure, and ensure that no other activity instances will enter. We modify the launchmode = "standard" of firstactivity and the launchmode = "singleinstance" of secondactivity. Since multiple stack structures are involved, we need to display the ID of the current stack structure in each activity, so we add the following code for each activity:

Then let's demonstrate the process:

We found that the two activity instances are placed in different stack structures. The schematic diagram of singleinstance is as follows:

When we jump from firstactivity to secondactivity, we re enable a new stack structure to place the secondactivity instance, and then press the back button to return to the original stack structure again; As shown in the lower part of the figure, jump to firstactivity again in secondactivity. At this time, the system will generate a firstactivity instance in the original stack structure, and then go back twice. Note that it does not exit, but returns to secondactivity. Why? This is because when we jump from secondactivity to firstactivity, our starting point becomes the stack structure where the secondactivity instance is located. In this way, we need to "return" to this stack structure. If we modify the launchmode value of firstactivity to any of singletop, singletask and singleinstance, the process will be as shown in the figure:

SingleInstance startup mode is probably the most complex mode. To help you understand, let me give you an example. If we have a share application, ShareActivity is entrance Activity and Activity for other applications. We set the Activity startup mode to singleInstance and then call it in other applications. We edit the configuration of shareactivity:

Then we start the activity in other applications as follows:

When we open shareactivity and press the back button to return to the original interface, shareactivity exists as an independent individual. If we open the share application at this time, we can see the results without creating a new shareactivity instance, because the system will automatically find it and use it if it exists. You can print taskid in shareactivity to see the effect. The schematic diagram of this process is as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>