Analysis of Activity startup mode

  • 2021-12-11 08:51:50
  • OfStack

Preface

Usually, when we start an activity, it is directly startActivity, which may not pay attention to the startup mode of the activity. By default, it is started in the default startup mode. But the startup mode is sometimes more important. For example, if you want an activity to be started only once without multiple instances, you may need to set it to singleTask mode. Therefore, it is necessary to know these 1 startup modes. At the same time, we should pay attention to 1, startup mode ≠ startup mode, which refers to display startup and implicit startup. Don't confuse display startup and implicit startup. I will have a special article to explain them later.

Brief introduction of task stack

To understand the startup mode, we must first understand the concept of task stack under 1. On the implementation of the task stack principle, I will not say here, here is a brief introduction to what is the task stack. The activity instances we start will be placed in a thing called task stack. We all know that the stack is characterized by "last in first out". For example, the task stack is a badminton tube, and the activity example is a badminton, and those put in can only be taken out first. So when we start an app, a task stack is automatically created, and then we drop the active instances into it. When we press back to destroy activities, these activities come out of the task stack in turn. Of course, an app can have multiple task stacks, for example, activities started with singleInstence are in a separate task stack. After understanding the concept of task stack, we can look at the four startup modes of activities.

Analysis of four startup modes of Activity

standard

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

singleTop

As the name implies, the top of the stack is single 1 instance. What do you mean? Suppose you start an ActivityA now, but there is already an ActivityA instance at the top of the stack at this time, then no new instance will be created at this time. However, if the same instance exists at the non-top of the stack, a new instance will be created. For example, the activity in the stack is now ABC, and A is at the top of the stack. Then start A at this time, it will not create another A activity, but execute onNewIntent method of A; However, if the C activity is started at this time, because the top of the stack is A instead of C, a new C instance will still be created, and the stack situation at this time is CABC.

singleTask

Single 1 task mode. This pattern means that only one instance can exist in the active startup stack, 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 now a stack Main, but you can assign a stack name dev to the active A, so when you start A, you will create a stack called dev. Therefore, singleTask means that when you start an activity with singleTask startup mode, if there is no identical instance in the stack, a new instance will be created and put into the stack; If there is the same instance in the specified stack, for example, ABC in the stack, and then you start B, then at this time, you will not create a new B instance, but put B at the top of the stack, push A out, and then execute 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 characterized by last-in, first-out. For example, if you put three badminton in the stack, can you only take out the top badminton if you want to get the middle badminton? By the same token, if you want to bring B to the top of the stack, you must push A out. Many readers may mistakenly think that it is BAC after startup, but it is actually BC, because A has to go out of the stack before B can come out. Similarly, if ADFBC is in the stack, this boot B is also BC, and all the above are off the stack.

singleInstance

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

Two Ways to Set Startup Mode

After understanding the four startup modes of the activity, let's look at 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 startup mode by specifying the launchMode parameter to the activity. For example:


 <activity android:name=".MainActivity"
      android:launchMode="singleInstance"/>

Dynamic setting

Dynamic setting is to specify the startup mode when starting the activity, for example:


Intent intent = new Intent();
intent.setClass(this,SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

As you can see, we specify the startup mode through the method intent. addFlags, which passes in 1 parameter to specify the startup mode, and the other parameters are:

FLAG_ACTIVITY_NEW_TASK: singleTask Mode FLAG_ACTIVITY_SINGLE_TOP: singleTop Mode FLAG_ACTIVITY_CLEAR_TOP: Clear all activities above this activity. 1 and singleTask1. But if your startup mode is standard, then this activity and all activities above it will be put out of the stack and a new instance will be created. For example, ABCD is now in the stack. When starting C in FLAG_ACTIVITY_CLEAR_TOP+standard mode, ABC will be cleaned up first. Yes, C will also be cleaned up, and then a new C will be created and put in. After execution, CD will be created.

Pit of special attention

singleInstance Returns Task Stack

Now simulate a scenario: There are now three active A, B, C. The startup mode of A and C is standard, and the startup mode of B is singleInstance. Start A, then B, then C. At this time, the question comes. If I press the return button at this time, will I return to B? The answer is to go back to A. Press 1 again to return to the desktop? The answer is to go back to B and press 1 again to return to the desktop. Actually, it is not difficult to understand. We all know that singleInstance will create a separate stack. When we start A, A is located in the stack First. When we start B, we will create a stack Second and put an B instance in it. If you start C again at this time, you will switch to the stack FIrst. Because only one stack can be placed in the stack First created by singleInstance, C will be placed in the stack First. When you press Back, the activities in the stack First will be put out of the stack in turn, and will not switch to the stack Second until all of them are put out. So pay attention to this point.

singleTask multitasking stack startup problem

This problem is the same as the essence of singleTop above. Simulate a scenario: There are now two stacks: First: ABC; Second: QWE. Stack First is located in the foreground and stack Second is located in the background. A is at the top of the stack. At this time, start W in singleTask mode. What will happen? First, you switch to the stack Second, then take Q off the stack, take W to the top of the stack, and execute onNewIntent method of W. At this time, pressing the return key will take the activities in the Second stack out of the stack in turn, and switch to the stack First after all the activities are out.

TaskAffinity and allowTaskReparenting Parameters of singleTask

We talked about specifying the name of the task stack to start for singleTask mode. How to specify it? You can specify the relevant attributes in AndroidManifest, as follows:


<activity android:name=".Main2Activity"
     android:launchMode="singleTask"
     android:taskAffinity="com.huan"
     android:allowTaskReparenting="true"/>

Explain these two parameters under 1 here

taskAffinity: Specifies the name of the task 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, usually set to true and used with parameter 1 above.

I mentioned earlier that you can assign a stack name to an singleTask activity, and when you start, you switch to that stack and put the new activity in. However, if you set the allowTaskReparenting parameter to false, you will not switch to the new stack. This parameter means whether the new activity can be transferred to the new task stack. Simply put: When we start an singleTask activity, the activity remains in the stack that started the activity. But if we specify the taskAffinity parameter, or if the activity started is an activity in another application, a new task stack will be created. If the parameter allowTaskReparenting is true, the activity will be placed in the new task stack. In this way, we should be able to understand. Therefore, these two are often used together.

Summarize

There are 4 kinds of active startup modes, each of which has different functions, which can be used according to specific needs, but the most important thing is to understand his implementation principle and how the stack changes, which is more important. After understanding this, those special situations are easy to understand.
What I said above is just simple use, and there is still a lot to know about the activity startup mode. It may be analyzed in the follow-up, and readers can also have a deep understanding by themselves.

References

"Exploration of Android Development Art"-Ren Yugang

The above is the detailed analysis of Activity startup mode, more information about Activity startup mode please pay attention to other related articles on this site!


Related articles: