Concept of pre –

  1. Task

    After each App is started, you can view the started App in the latest Task list. What you see here is actually the Task list, and you can see the tasks of each App.

    The Task stack consists of activities that are started. Each Activity startup is associated with a Task.

  2. TaskAffinity

    TaskAffinity: each Activity has its own TaskAffinity, which is the Application TaskAffinity by default, and the Application TaskAffinity is the App package name by default.

    It acts as a grouping of activities. The TaskAffinity in each Task is the same, that is, the TaskAffinity for the first Activity started. TaskAffinity can be customized if an Activity specifies a different TaskAffinity than the Application and is launched in SingleTask or SingleInstance mode. When it is started in the App, it will run into the TaskAffinity Task, and if the Task is not found, it will create a new Task.

Other intellectual

  1. allowTaskReparenting

    An Activity can specify this property in its Manifest, which is false by default. When true, it allows the Activity to be moved to the Task that started it, and when you click the App that the Activity belongs to, the Activity is returned to the Task and the Activity in the Task that started it disappears.

  2. Task switching

    When the Activity started is not a Task started, that is, the TaskAffinity is different, Task switching will occur and there will be an animation of Task switching, which reminds the user that it is a cross-task operation.

The four types of Launch Mode

  1. Standard Default type

    This Mode is used when the Activity does not specify Launch Mode. Regardless of whether there is an instance of the Activity, the startup is created and added to the Task stack that started it.

  2. SingleTop stack top reuse mode

    If the Activity is at the top of the stack, the new instance will not be reused. If the Activity is not at the top of the stack, the new instance will be added to the Task that started it.

  3. SingleTask stack reuse pattern

    If the Activity is in the Task stack that started it, it will not create a new instance, reuse the Activity in the stack, and let the activities above it out of the stack, which has a Clear Top effect. If there is no instance of the Activity in the starting Task stack, a new Activity will be created. If the TaskAffinity of the Activity does not match the TaskAffinity of the starting Task stack, a new Task will be created and the Activity will be added to the top of the stack. A special feature of this mode of Activity is that it follows its TaskAffinity Task instead of starting its Task. For example: App1 :A ->B App2:A2-> B2-c2, B2 is the startup mode of SingleTask, and the other modes are default. App2 has an instance of this Activity in its stack, so it will reuse it directly and remove the Activity above it. App2: A2 – > B2; This Task is then superimposed on top of App1’s Task to form A fallback stack: A->B->A2->B2.

  4. SingleInstance Single-instance mode

    More powerful than SingleTask, only one Task is allowed, and only one Activity is allowed in that Task. That is, even if TaskAffinity’s SingleInstance Activity is not specified, it will create a Task for it when it is first started, and then re-use that Task when it is started. The Activity’s Task is superimposed on top of the Task that started it. The Activity’s TaskAffinity is the same as its Application, which means that there are two tasks with the same TaskAffinity, and the two overlapping tasks are split when the recent Task button is clicked or switched to the background. However, only one Task of each App will appear in the recent Task list. That is to say, when you set up the recent Task to check the Task, you will find that the SingleInstance Task is missing and the Activity is not found. Although you can’t see it, it still exists.

Applicable scenario

Boot mode Applicable scenario
Standard For use in App, normal Activity, finish when finished. A logically independent Activity that can be used across apps. Once started, it has no relationship with the App to which it belongs.
SingleTop The usage scenario is similar to Standard, with a top of the stack feature that does not rebuild.
SingleTask Within the App is used, is generally MainActivity, reuse within the stack, ensure that there is only one Activity instance, due to the Clear Top attributes, can be used in the graceful exit application (finish above it off so the Activity). Cross-app use, which provides the feature when an Activity is started by an external App. The Task to which the Activity belongs is superimposed on the Task that started it, forming a rollback stack. For example, when App1 starts App2’s SingleTask Activity, press the back key to roll back, it will first roll back App2’s stack, and then App2’s Activity will be off the stack, and App1’s Activity will be off the stack. Generally used for an Activity that is provided to an external App to start and that logically depends on its own App, so that when you switch to the App to which the Activity belongs, the Activity still exists.
SingleInstance Generally used across apps, it provides external apps to start an Activity. Logically independent activities will provide independent tasks to host the Activity. After the external App has processed it, it returns. Since the Task has only one Activity, it returns directly to the Task that started it. It does not add unwanted activities to the Task, resulting in redundant activities in your rollback stack.
other
allowTaskReparenting An Activity that can be launched for an external App will be added to the external AppTask stack, but the entire Activity of its original Task will not be added to the fallback stack. And when the original App is opened again, the Activity returns to the original Task. This works for activities that can be provided to an external startup and that rely on the logic of the original App. Cutting back to the original App will still return the Activity without disrupting the fallback logic of the App that started it.

conclusion

The startup mode of an Activity is not difficult, but it is easy to understand when you understand the concepts of Task and TaskAffinity and the startup mode of SingleTask and SingleInstance. The activities launched by each App are grouped and abstracted into a Task stack, which is determined by TaskAffinity. When SingleTask and SingleInstance are started, if the Activity’s TaskAffinity is different from that of the Task that started it, cross-task operations will occur. First, the Task to which the Activity belongs will be checked to see if it exists, and if it does not, a new Task will be created. These launch mode features determine how an Activity should be started and switched on demand, whether it should be switched within or across tasks, whether another Task’s Activity should be added to the fallback stack, or whether only one external Activity should be added to the fallback stack, and so on.