Four Startup Modes of Activity in Android Programming

  • 2021-07-01 08:15:27
  • OfStack

This paper describes four startup modes of Activity in Android programming. Share it for your reference, as follows:

There are four startup modes of Activity, which are:

standard
singleTop
singleTask
singleInstance

You can set the corresponding startup mode for Activity according to the actual requirements, thus avoiding the problem of creating a large number of repeated Activity.

To set the startup mode of Activity, only the corresponding one in AndroidManifest. xml is needed < activity > Tag sets the android: launchMode attribute, for example:


<activity 
  android:name=".A1" 
  android:launchMode="standard" /> 

The following are the functions of these four modes:

standard

Default mode, you don't need to write configuration. In this mode, a new instance is created by default. Therefore, in this mode, there can be multiple identical instances, and multiple identical Activity superimposition is also allowed.

For example:

If I have an Activity named A1, it has a button to jump to A1. Then if I click the button, I will start a new Activity A1 stacked on top of the A1 just now, and if I click again, I will start another one on top of it...

Point back will exit in order of stack.

singleTop

There can be multiple instances, but multiple identical Activity overlaps are not allowed. That is, if the same Activity is started while Activity is at the top of the stack, no new instance is created, but its onNewIntent method is called.

For example:

If I have two Activity named B1 and B2, the contents and functions of the two Activity are exactly the same, and both of them have 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 intended to open B2, I actually only called the first onNewIntent method)
If the order I intend to open is B1- > B2- > B1- > B2, then the actual opening order is the same as the intention, which is B1- > B2- > B1- > B2.

singleTask

There is only one instance. When starting it in the same application, if Activity does not exist, a new instance will be created in the current task, and if it does, other Activity destory above it in task will be dropped and its onNewIntent method will be called.

If it is started in another application, a new task will be created and this Activity will be started in this task. singleTask allows other Activity to coexist with it in an task, that is, if I open a new Activity in an instance of singleTask, this new Activity will still be in task of an instance of singleTask.

For example:

If my application has three Activity, C1, C2, C3, and three Activity that can start each other, where C2 is singleTask mode, then no matter how I click Start in this program, such as C1- > C2- > C3- > C2- > C3- > There may be multiple instances of C1-C2, C1 and C3, but there will only be one instance of C2, and all three Activity are in the same task.

But C1- > C2- > C3- > C2- > C3- > C1-C2, this operation process should be as follows, because singleTask will remove other Activity destory above 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- > C3- > C1 C1- > C2

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

If there is one activity and taskId in other applications Other, and C2 is turned on from it, taskIdI of C2 will not be 200, for example, taskId of C2 is 201, then C1 and C3 are turned on from C2, and C2 and C3 are still 201.

Note: If you click home at this time and then open Other, you will find that the content displayed at this time will definitely be the content in Other application, not one of C1 C2 C3 in our application.

singleInstance

There is only one instance, and this instance runs independently in an task. This task only has this instance, and no other Activity is allowed to exist.

For example:

There are three ActivityD1, D2 and D3 in the program, and three Activity can start each other, among which D2 is singleInstance mode. Then the program starts running from D1. Assuming that taskId of D1 is 200, when D2 is started from D1, D2 will start a new task, that is, D2 and D1 are not running in one task. Assume that taskId of D2 is 201, and when D3 is started from D2, taskId of D3 is 200, which means that it is pressed into the task stack started by D1.

If D2 is opened in another application, if taskId of Other is 200, if D2 is opened, D2 will create a new task to run, and if its taskId is 201, then if D1 or D3 is started from D2 at this time, another task will be created. Therefore, if the operation step is other- > D2- > D1, this process involves three task.

More readers interested in Android can check the topics of this site: "Summary of activity Operation Skills for Android Programming", "Summary of Android Communication Methods", "Summary of Android Debugging Skills and Common Problem Solutions", "Introduction and Advanced Tutorial of Android Development", "Summary of Android Basic Component Usage", "Summary of Android Layout layout Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: