Understand the four startup modes of Android

Posted by cdwhalley.com on Fri, 31 Dec 2021 17:51:59 +0100

1: Foreword

The four modes are standard, singletop, singletask and singleinstance. You should clarify a concept. First, what does single want. Each application app has a package name, and then each application will have a task. This task corresponds to the package name. If you want to create a new task, the name must be different.

  • An application will have a package name. By default, there is a task with the package name in the application
  • task is a concept and its implementation is stack stack.
  • task is used to manage activities. When an activity is transferred to another activity, it will stack the new activity and follow the first in first out rule.
  • The activity in the task can span applications. For example, jump to wechat in the app, and then press the return key to return to your own application.

 

 

2: How to change the startup mode

  1. Set the activity in the manifest
  2. Change the flag when Intent starts activity

 

3: Start directly with singleTop

Three activities, activity1 can jump to activity2, and activity2 can jump to activity3

1: In the first case, there is an instance at the top of the stack

Stack bottom--------------------------------------->Stack top
acitivity1     activity2    acitivity3   

At this time singleTop Mode start activity3,Reuse the old instance. Note: call by the way activity3 of onNewIntent()method

Stack bottom--------------------------------------->Stack top
acitivity1     activity2    acitivity3  

2: In the second case, there is no instance at the top of the stack

Stack bottom--------------------------------------->Stack top
acitivity1    acitivity3    activity2

At this time singleTop Mode start activity3,Create a new instance

Stack bottom--------------------------------------->Stack top
acitivity1    acitivity3    activity2  activity3(new)

 

 

4: Simple single task

Note to set in the manifest

android:launchMode="singleTask"

(I find it useless to set the flag of intent or not. The key is to set this. Setting only flag is useless)

1: In the first case, taskAffinity is not set in the manifest

If the current task does not have an instance of this activity, create one and put it on the top of the stack; If there is an instance, all the activities on that instance will be taken out of the stack (destroyed), and the of the activity instance to be started will be called: onRestart, onStart, onResume

Stack bottom--------------------------------------->Stack top
acitivity1    acitivity3    activity2

At this time singleTast Mode start activity3,But no taskAffinit
 I'll take what I already have activity3 above activity,Namely activity2 Out of stack, destroyed
 And call the existing activity3 of onRestart,onStart, onResume

Stack bottom--------------------------------------->Stack top
acitivity1    acitivity3 (old)

That is, singleTask ensures that it is unique in the current task instance

 

2: In the second case, taskAffinity is set in the manifest

2.1: this task has been created

Stack bottom--------------------------------------->Stack top

task1:
acitivity1    acitivity3    activity2

task2:
acitivity1    acitivity3    activity2

with singleTask Mode start activity3,appoint task2,The performance is the same as the first case
 Put the top activity All out of the stack

task1:
acitivity1    acitivity3    activity2

task2:
acitivity1    acitivity3(old)

2.2: this task has not been created

Stack bottom--------------------------------------->Stack top

task1:
acitivity1    acitivity3    activity2


with singleTask Mode start activity3,appoint task2
 Will create task2,And create a new instance and put it in

task1:
acitivity1    acitivity3    activity2

task2:
activity3(new)

ps: other activities started by activity3 after that, unless a new task is specified, they are all in the stack where activity3 is located, that is, the so-called "marry chicken with chicken and marry dog with dog"


The name of singleTask is very misleading. I think the name singleAtTask is more appropriate.

 

5: Simpler singleInstance

The activation mode is singleInstance. There can only be one instance in the whole mobile phone. When calling again, the instance will be found and transferred to the foreground.

For example, if activity1 has been started in singleInstance mode, move it to the foreground (if it already exists).

  • activity1 belongs to a unique task, which is the only one
  • activity1 starts other activities. If the activity indicates taskAffinity (even if it is not in the single task mode), it will be created in the indicated task. If taskAffinity is not specified, it will find the task with the application package name by default. The logic here is very similar to that of single task.

 

 

 

 

 

reference material: https://blog.csdn.net/zhangjg_blog/article/details/10923643

     https://www.kancloud.cn/alex_wsc/android_art/1828110

Topics: Android