Four startup modes of activity in Android programming

This article describes four startup modes of activity in Android programming. Share with you for your reference, as follows:

There are four ways to start an activity:

standard singleTop singleTask singleInstance

You can set the corresponding startup mode for the activity according to the actual needs, so as to avoid creating a large number of duplicate activities.

To set the startup mode of an activity, you only need to set the Android: launchmode attribute in the corresponding < activity > tag in androidmanifest.xml, for example:

Here are the functions of these four modes:

standard

The default mode can be configured without writing. In this mode, a new instance will be created by default. Therefore, in this mode, there can be multiple identical instances, and multiple identical activities can be superimposed.

For example:

If I have an activity named A1, there is a button on it to jump to A1. Then, if I click the button, I will start a new activity A1 superimposed on the previous A1, and then click again, I will start a new activity A1 on it

Click back to exit in stack order.

singleTop

There can be multiple instances, but multiple overlapping of the same activity is not allowed. That is, if an activity starts the same activity when it is at the top of the stack, it will not create a new instance, but will call its onnewintent method.

For example:

If I have two activities named B1 and B2, the contents and functions of the two activities are exactly the same. There are two buttons to jump to B1 or B2. The only difference is that B1 is standard and B2 is singletop. If the order I intend to open is B1 - > B2 - > B2, the actual opening order is B1 - > B2 (the last time I intend to open B2, only the previous onnewintent method is actually called). If the order I intend to open is B1 - > B2 - > B1 - > B2, the actual opening order is consistent with the intention, which is B1 - > B2 - > B1 - > B2.

singleTask

There is only one instance. When starting him in the same application, if the activity does not exist, a new instance will be created in the current task. If it does exist, other activities above it in the task will be destroyed and its onnewintent method will be called.

If you start it in another application, you will create a new task and start the activity in the task. Singletask allows other activities to coexist with it in a task. That is, if I open a new activity in the instance of singletask, the new activity will still be in the task of the instance of singletask.

For example:

If there are three activities in my application, C1, C2 and C3, which can start each other, and C2 is in singletask mode, no matter how I click to start in this application, for example: C1 - > C2 - > C3 - > C2 - > C3 - > C1-C2, there may be multiple instances of C1 and C3, but there will only be one C2, and the three activities are in the same task.

However, C1 - > C2 - > C3 - > C2 - > C3 - > C1-C2. The actual operation process should be as follows, because singletask will destroy other activities in the task.

Operation: C1 - > C2 C1 - > C2 - > C3 C1 - > C2 - > C3 - > C2 C1 - > C2 - > C3 - > C2 - > C3 - > C1 C1 - > C2 - > C3 - > C2 - > C3 - > C1-C2 actual: C1 - > C2 C1 - > C2 - > C3 C1 - > C2 C1 - > C2 C1 - > C2 - > C3

If another application opens C2, a new task will be started.

If there is an activity in other applications, taskid is 200, and C2 is opened from it, then the taskid of C2 will not be 200. For example, if C2's taskid is 201, then C1 and C3 are opened from C2, then the taskid of C2 and C3 will still be 201.

Note: if you click home and then open other, you will find that the content in other application will be displayed, not one of C1, C2 and C3 in our application.

singleInstance

There is only one instance, and this instance runs independently in a task. This task has only this instance, and no other activities are allowed.

For example:

The program has three activityd1, D2 and D3. The three activities can start each other, among which D2 is in singleinstance mode. Then, the program starts running from D1. Assuming that the taskid of D1 is 200, when starting D2 from D1, D2 will start a new task, that is, D2 and D1 do not run in the same task. Assuming that D2's taskid is 201, when starting D3 from D2, D3's taskid is 200, that is, it is pressed into the task stack started by D1.

If D2 is opened in another application, assuming that the taskid of other is 200, D2 will create a new task to run. Assuming that its taskid is 201, if D1 or D3 is started from D2, another task will be created. Therefore, if the operation step is other - > D2 - > D1, this process involves three tasks.

For more Android related content, interested readers can view the special topics of this site: activity operation skills summary of Android programming, Android communication mode summary, Android debugging skills and common problem solving methods summary, introduction and advanced teaching of Android development, usage summary of Android basic components, Android layout skills summary, and Android control usage summary

I hope this article will help you in Android programming.

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