Let's take a look at the Activity startup mode

The Mini Program Development Tutorial mainly introduces the related information of the Activity startup mode.


Recommended (free): Mini Program Development Tutorial

Preface
Usually when we start an activity, we start Activity directly. Maybe we don’t pay attention to the activity’s startup mode. By default, we start with the default Mode starts. But the startup mode is sometimes more important. For example, if you want to start an activity only once and don't have multiple instances, then you may need to set it to singleTask mode. So it is necessary to understand these startup modes. At the same time, it should be noted that the startup mode ≠ the startup mode. The startup mode refers to the display startup and the implicit startup. Don't be confused. I will have a dedicated article to explain the display startup and implicit startup.

About task stack introduction
To understand the startup mode, we must first understand the concept of task stack. Regarding the implementation principle of the task stack, I won't talk about it here. Here is a brief introduction to what a task stack is. The activity instances we start will be placed in something called the task stack. We all know that the stack is a "last in, first out" feature. For example, the task stack is a badminton tube, and the activity instance is a badminton, which can only be taken out first. So when we start an app, a task stack is automatically created, and then we throw activity instances in it. When we press Return to destroy the activities, these activities will come out of the task stack in turn. Of course, an app can have multiple task stacks. For example, an activity started using singleInstence is in a separate task stack. After understanding the concept of the task stack, then you can take a look at the four start modes of activities.

Analysis of the four startup modes of Activity
standard
This is the standard startup mode, which is the default startup mode. Every time an activity in this startup mode is started, a new instance is created and placed on the stack, regardless of whether the same instance already exists in the stack. This is also the easiest to understand.

singleTop
As the name suggests, the top of the stack is a single instance. What does that mean. Suppose you start an ActivityA now, but there is already an ActivityA instance at the top of the stack at this time, then at this time, no new instance will be created. But if the same instance exists on the non-top of the stack, a new instance will still be created. For example, now the activity in the stack is ABC, and A is at the top of the stack. Then start A at this time, will not create another A activity, but execute A's onNewIntent method; but if the C activity is started at this time, because the top of the stack is A and not C, then a new C instance will still be created , The stack situation at this time is CABC.

singleTask
single task mode. This mode means that there can only be a single instance in the startup stack of the activity, regardless of whether it is at the top of the stack. Different from other startup modes, this startup mode can specify the stack to start. For example, there is a stack Main, but you can assign a stack name dev to activity A, and then a stack called dev will be created when A is started. So singleTask means that when you start an activity whose startup mode is singleTask, if there is no same instance in the stack, then a new instance will be created and put into the stack; if the same instance exists in the specified stack, for example There is ABC in the stack, and then you start B. At this time, you will not create a new instance of B. Instead, you will put B on the top of the stack and push A out, and then execute the onNewIntent method of B. At this time, the stack situation is BC.
Careful readers will find "top out". Yes, we all know that the stack is a last-in, first-out feature. For example, if you put 3 shuttlecocks in the tube, then if you want to get the shuttlecock in the middle, can you only pull the upper one out first? The same principle , If you want to bring B to the top of the stack, you must top A. There may be many readers who mistakenly think that it is BAC after startup, but it is actually BC, because A has to be released before B can come out. In the same way, if there is ADFBC in the stack, this start B is also BC, and all the above are popped out of the stack.

singleInstance
Singleton mode. This is an enhanced version of singleTask. He will create a new stack and put this new instance into it, and this stack can only put this active instance. So when the activity is repeatedly started, as long as it exists, it will call the activity onNewIntent method and switch to this stack, and will not create a new instance.

Two ways to set the startup mode After
understanding the four startup modes of the activity, let's see how to specify the startup mode for him.

Static setting
Static setting is to set the startup mode for specific activities in AndroidManifest. Set the launch mode by specifying the launchMode parameter to the activity. For example:

1

2

<activity style='margin: 0px; padding: 0px; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: "Helvetica Neue" , Helvetica, "PingFang SC", Microsoft Yahei, Tahoma, Arial, sans-serif; font-size: 14px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(238, 238, 238); -webkit-text-stroke-width: 0px; font-variant-ligatures: normal; font-variant-caps: normal; text- decoration-style: initial; text-decoration-color: initial;' 
     android:launchMode="singleInstance"/>

Dynamic setting
Dynamic setting is to specify the launch mode when the activity is launched, for example:

1

2

3

4

Intent intent = new Intent ();

intent.setClass(this,SecondActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

You can see that we use the intent.addFlags method to specify the startup mode. This method passes in a parameter to specify the startup mode. Other parameters are:

FLAG_ACTIVITY_NEW_TASK: singleTask mode
FLAG_ACTIVITY_SINGLE_TOP : SingleTop mode
FLAG_ACTIVITY_CLEAR_TOP: Clear all activities above the activity. Generally used together with singleTask. But if your startup mode is standard, then this activity and all activities above it will be popped out of the stack and a new instance will be created and put in. For example, now there is ABCD in the stack. When starting C in FLAG_ACTIVITY_CLEAR_TOP+standard mode, first clean up ABC. Yes, C will also be cleaned up, and then create a new C and put it in. After execution, it will be CD.
Pay special attention to the
singleInstance return to the task stack
Now simulate a scenario: there are now three activities A, B, and C. The startup mode of A and C are both standard, and the startup mode of B is singleInstance. Start A first, then start B, and then start C. At this time, the question is, if I press the back button at this time, do I go back to B? The answer is back to A. Click again to return to the desktop? The answer is to go back to B, click again to return to the desktop. In fact, it is not difficult to understand. We all know that singleInstance will create a separate stack. When we start A, A is in the stack First, and when we start B, a stack Second will be created and the B instance will be put into it. At this time, start C again, and it will switch to the stack FIrst. Because the stack created by singleInstance can only be placed in one, C will be placed in the stack First. When the return is pressed, the activities in the stack First will be sequentially popped out. It will not switch to the second stack until it is all out. So pay attention to this point.

SingleTask multitasking stack startup problem
The essence of this problem is the same as singleTop above. Simulate a scenario: Now there are two stacks: First: ABC; Second: QWE. The stack First is in the foreground, and the stack Second is in the background. A is at the top of the stack. What happens when W is started in singleTask mode at this time? First, it switches to the stack Second, then pops Q from the stack, lifts W to the top of the stack, and executes W's onNewIntent method. At this time, pressing the return key will pop the activities in the Second stack one by one, and switch to Stack First after all the activities are completed.

TaskAffinity and allowTaskReparenting parameters of singleTask.
Earlier we talked about specifying the name of the task stack to be started for the singleTask mode. How to specify it? You can specify related attributes in AndroidManifest, as follows:

1

2

3

4

<activity style="margin: 0px; padding: 0px;" 
     android:launchMode="singleTask"

     android:taskAffinity="com.huan"

     android: allowTaskReparenting ="true"/>

Here to explain these two parameters

taskAffinity: specify the task The name of the stack. The default task stack is the package name, so it cannot be named after the package name.
allowTaskReparenting: This parameter indicates whether you can switch to a new task stack. It is usually set to true and used with the above parameters.
I mentioned earlier that you can specify a stack name for the singleTask activity, and then when it starts, it will switch to that stack and put the new activity into it. But if you set the allowTaskReparenting parameter to false, it will not switch to the new stack. This parameter means whether the new activity can be transferred to the new task stack. To put it simply: when we start a singleTask activity, the activity remains in the stack that started his activity. But if we specify the taskAffinity parameter, or the activity started is an activity in another application, a new task stack will be created. If the allowTaskReparenting parameter is true, then the activity will be placed in the new task stack. This should be clear. So these two are often used together.

Summary
There are four startup modes for activities, each of which has different functions and can be used according to specific needs, but the most important thing is to understand its implementation principle and how the stack changes. This is more important. After understanding this, those special situations are easy to understand.
What I talked about above is just a simple use, there is a lot to know about the activity start mode. It may be analyzed later, and readers can also learn more about it by themselves.

Related free recommendation: programming video course

Guess you like

Origin blog.csdn.net/zl17822307869/article/details/113065812
Recommended