This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Activity Start mode

In Android development, activities are managed as stacks. When the system creates multiple activities, it pops them into the task stack. The current foreground Activity is placed at the top of the task stack according to the “first in, last out” position. However, Android has a variety of startup modes for starting activities, which allows for more flexible system behavior.

Standard Default mode

With standard, the default startup mode for an Activity is a single instance. You can have multiple instances of the same Activity in a task stack and they pop into the same task stack.

For example, in the following example, activities launched in Standard mode are pushed into Task1 and ActivityA can create multiple instances:

flowchart LR
subgraph Task1
id1(ActivityA) --> ActivityB --> id2(ActivityA) --> ActivityC
end

SingleTop stack top reuse mode

In singleTop mode, if the Activity is already at the top of the task stack, it will not be created. Instead, it will reuse the current Activity and call the onNewIntent method in the Activity to get the Intent information that started it.

1. The startup mode shown below is singleTop’s ActivityC at the top of the stack. No new ActivityC will be created when ActivityC is started.

flowchart LR
subgraph Task1
ActivityA --> ActivityB --> ActivityC -->|onNewIntent| ActivityC
end

2. As shown below, ActivityC of singleTop is not at the top of the stack. When ActivityC is started after ActivityA, a new ActivityC will be created.

flowchart LR
subgraph Task1
id1(ActivityA) --> ActivityB --> id3(ActivityC) --> id2(ActivityA) --> ActivityC
end

PS: This mode can avoid multiple clicks to start two identical activities, and is suitable for popover activities

SingleTask stack reuse pattern

SingleTask can be understood as singleTask stack single instance mode with singleTop function. There is only one Activity in a task stack.

1. For example, in singleTask mode, ActivityD starts a new task stack that is not in Task1, and starts a new ActivityD at the top of the stack.

flowchart LR

subgraph Task2
ActivityD -->|onNewIntent| ActivityD
end

subgraph Task1
id1(ActivityA) --> ActivityB --> id3(ActivityC)
end

2. For example, ActivityD in singleTask mode is not at the top of the stack after startup. If you start a new ActivityD in singleTask mode at the same time, the ActivityD in singleTask mode will be at the top of the stack according to the single-task stack and single-instance multi-use. Also, singleTask with clearTop removes activities (ActivityC, ActivityF) from the ActivityD stack. Finally, the Task1 stack is down to ActivityA, ActivityB, and ActivityD

flowchart LR

subgraph Task1
id1(ActivityA) --> ActivityB --> ActivityD x--x C(ActivityC) x--x F("ActivityF") --> ActivityD
end

SingleInstance singleInstance mode

SingleInstance is an enhanced single-instance pattern of singleTask. The Activity in singleInstance mode is the only member of the Task. Therefore, all activities started in this mode have their own independent tasks.

Intent logo

Used to change the startup mode of the ActivityCopy the code
  1. FLAG_ACTIVITY_NEW_TASK

    Equivalent singleTask

  2. FLAG_ACTIVITY_SINGLE_TOP

    As singleTop

  3. FLAG_ACTIVITY_CLEAR_TOP

    Prerequisites If the Activity to be started is in a Task, all activities at the top of the current Task are destroyed and passed to the Activity to be resumed through onNewIntent instead of creating a new one.

Clean back stack

  1. alwaysRetainTaskState

    If the root Activity instance of the Task is set to true, the Activity in the Task will remain on the stack for a long time

  2. clearTaskOnLaunch

    The opposite of alwaysRetainTaskState. The Task root Activity instance is set to true to leave the Task for a short period of time and return with only the root Activity remaining

  3. finishOnTaskLaunch

    Similar to clearTaskOnLaunch, set to true if the user leaves and returns to the Task, the Task will no longer exist

reference

  • Learn about tasks and return stacks