The understanding of activity startup mode singleTask

1. The understanding of activity startup mode singleTask

2. Task and Activity stack

Task is a collection of some activities, stored in the form of Activity stack. Therefore, Task is conceptual, and Activity stack is physical.

It can be said that a newly started Task means a new Activity stack is created to store the Activity in this Task.

3. SingleTask setting method

  • The manifest file
    adds attributes to the activity node:
<activity
    android:name="com.example.test.SecondActivity"
    android:launchMode="singleTask"
/>

  • intent set FLAG
Intent intent = new Intent(this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

4. The meaning of singleTask (function)

Setting the startup mode of an activity to singleTask means that when the framework starts the activity, it marks it as being able to be started in a new task . As for whether to start in a new task, that is not necessarily true! It requires conditions, what conditions? Please see the breakdown below~

4.1. Examples

4.2. Example 1

Assume the following jump relationship:

MainActivity -> SecondeActivity -> ThirdActivity

Among them, SecondActivity is set to the startup mode of singleTask, MainActivity and ThirdActivity do not do anything, keep the default, that is, the standard mode

Then after jumping to ThirdActivity, the current Activity stack situation from top to bottom is:

ThirdActivity、SecondeActivity、MainActivity

That is to say, it is still in the normal stacking order, the same as the standard mode. It is not according to Google's official explanation. It is easy to misunderstand that a new Task is created and the new stack is used to store SecondActivity. This is a misunderstanding.

So, is it really the same as the standard mode?

There are still differences, otherwise, why do you use this mode?

The difference is illustrated with the following example:

4.3. Example 2

Assume the following jump relationship:

MainActivity -> SecondeActivity -> ThirdActivity -> ForthActivity ->SecondeActivity

Among them, SecondActivity is set to the startup mode of singleTask, and the rest of the activities do not do any operations, and keep the default, which is the standard mode.

Then after jumping to SecondActivity, the current Activity stack situation from top to bottom is:

SecondeActivity、MainActivity

That's right, the ThirdActivity and ForthActivity above SecondActivity have been destroyed.

In other words, the existing SecondActivity in the stack is reused, and all the activities on it are cleared, so that it is exposed at the top.

By the way, the start of the last SecondActivity did not call the onCreate, onStart, and onResume life cycle methods, but instead called the onNewIntent method.

4.4. Summary

The activity marked as singleTask, at startup, if there is no instance currently, just like the standard mode, it will be directly pushed onto the current activity; if there is already an instance, all activities on the target Activity instance will be cleared, thereby removing The target Activity is exposed to the top and its onNewIntent method is called

5. taskAffinity attribute

Both of the above examples are within the scope of our knowledge and are relatively familiar. But this taskAffinity must be unfamiliar.

taskAffinity is to mark which task the current activity is attached to.

5.1. Configuration method


  • Configure this attribute in the activty node in the manifest file . The taskAffinity value is a string used to uniquely identify a Task.

By default, that is, not configured, taskAffinity takes the package name of the application.

 <activity android:name="com.example.test.SecondActivity"
             android:launchMode="singleTask"
             android:taskAffinity="com.example.test.second">
        </activity>

5.2. Meaning (function)

Working with singleTask means that the activity marked by these two attributes can be assigned to a new task.

5.3. Examples

Assume the following jump relationship:

MainActivity -> SecondeActivity -> ThirdActivity

Among them, SecondActivity is set to the singleTask startup mode, and the taskAffinity property is configured, MainActivity and ThirdActivity do not do anything, keep the default, that is, the standard mode

Then after jumping to ThirdActivity, the current Activity stack situation from top to bottom is:

Task1: ThirdActivity, MainActivity

Task2: SecondeActivity

As you can see, SecondActivity is created in the new stack.

Therefore, the answer to the previous question can be obtained. Under what conditions will the activity set as singleTask be created to a new task?

The answer is when another taskAffinity attribute value is assigned at the same time.

5.4. Rollback sequence

Now the activity in onResume is ThirdActivity, what if you press the return key at this time? What will the display order of activities be?

The answer is as in the order of the Task1 and Task2 stacks above. After ThirdActivity pops up, MainActivity is displayed; press the return key again, MainActivity pops up, and SecondActivity is displayed.

It can be seen that the newly created Task is at the bottom.

5.5. Summary

  1. Set the startup mode to singleTask, when the framework starts the activity, it will only mark it as startable in a new task. As for whether to start in a new task, it is also subject to other conditions. This condition is the taskAffinity attribute configuration It is a string other than the package name. If there is no configuration, the default is the current package name, and the operation is performed in the current task.
  2. When starting a singleTask Activity instance, if such an instance already exists in the system, it will be scheduled to the top of the task stack, and clear all the activities on it in the task it is currently in.
    If it does not exist, it depends on whether the Task corresponding to the taskAffinity attribute exists. If it exists, push the Activity into the Task inner stack; otherwise, create a new Task, and then push the Activity into the Task inner stack.
  3. The singeTask mode ensures that there is only one activity instance in a Task

Reference article: https://blog.csdn.net/zhangjg_blog/article/details/10923643

Guess you like

Origin blog.csdn.net/fxjzzyo/article/details/109293098