Android foundation summary part 2: four launchmodes of activity

Today we are going to talk about the four launchmodes of activity.

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.

Activity has the following four launchmodes:

1.standard

2.singleTop

3.singleTask

4.singleInstance

We can do it at Android manifest The Android: launchmode attribute of XML configuration < activity > can be one of the above four.

Let's introduce the four lanchmodes one by one 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:

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:

This is the standard startup mode. No matter whether there is an existing instance or not, a new instance is generated.

2.singleTop

On the basis of 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:

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:

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 Android: launchmode = "singletask" of firstactivity. The results of the demonstration are as follows:

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.singleInstance

This startup mode 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:

If we modify the launchmode value of firstactivity to any of singletop, singletask and singleinstance, the process will be as shown in the figure:

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:

Original link: http://blog.csdn.net/liuhe688/article/details/6754323

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>