Activity Startup Mode and Markup Bits

Keywords: Android xml shell

Activity startup mode

Before we talk about active startup mode, we need to understand the concept of active stack. The activity task stack is a "last in, first out" stack structure. That is, when a new activity instance is started, it is pushed onto the top of the stack. When the activity exits by pressing the return key, its instance is stacked from the task stack. The task stack is divided into foreground task stack and background task stack. The activity in the background task stack is in the suspended state. Users can switch the background task stack to the foreground again.

The startup mode of activity is to match the working principle of the stack to meet the daily business needs. There are four modes of activation: standard, singleTop, singleTask and singleInstance. Standard is the default startup mode for activity.

Standard: standard mode (default mode)

In this mode, activity creates a new instance every time it starts, and the stack is stacked regardless of whether the instance exists in the task stack or not. If activityA and activityB are both standard modes, activityA starts activityB, then the top of the stack is activityB, and below is activityA.

By default, the activity of the standard mode will enter the task stack to which its activity is started. To start the activity, a Context of non-activity type needs to add a tag bit FLAG_ACTIVITY_NEW_TASK, which is to create a new task stack for it. The boot mode of this flag bit is actually started in singleTask mode. (As will be explained later, skip marker knowledge points here first)

Command line view stack activity list information

Example: Executing jump activityA - activityB - activityC - activityA in the same APP without marker bits, they are all standard modes. In theory, the stack should be ABCA.

Command line execution:

 adb shell dumpsys activity

Command line query stack information contains a lot of information and miscellaneous, here we mainly look at
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
The information below this line is Hist_ 3:- Hist_ 2:- Hist_ 1:- Hist_ 0:. The order of display from top to bottom in the stack is Hist_ 3 at the top of the stack.

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #1:
    Task id #319
      TaskRecord{22fe5578 #319 A=com.jzf.myactivity U=0 sz=4}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.jzf.myactivity/.ActivityA }
        Hist #3: ActivityRecord{38af6616 u0 com.jzf.myactivity/.ActivityA t319}
          Intent { cmp=com.jzf.myactivity/.ActivityA }
          ProcessRecord{37252451 3045:com.jzf.myactivity/u0a58}
        Hist #2: ActivityRecord{29295492 u0 com.jzf.myactivity/.ActivityC t319}
          Intent { cmp=com.jzf.myactivity/.ActivityC }
        Hist #1: ActivityRecord{4dcc45c u0 com.jzf.myactivity/.ActivityB t319}
          Intent { cmp=com.jzf.myactivity/.ActivityB }
        Hist #0: ActivityRecord{2bc3c1f6 u0 com.jzf.myactivity/.ActivityA t319}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.jzf.myactivity/.ActivityA }

    Running activities (most recent first):
      TaskRecord{22fe5578 #319 A=com.jzf.myactivity U=0 sz=4}
        Run #0: ActivityRecord{38af6616 u0 com.jzf.myactivity/.ActivityA t319}

    mResumedActivity: ActivityRecord{38af6616 u0 com.jzf.myactivity/.ActivityA t319}

You can also see from Running activities (most recent first): below this line, the number of activities in the stack sz=4, and the activity A at the top of the stack.

singleTask: In-stack Reuse Mode

This is a single instance mode. As long as the activity of the mode exists on a stack, it will not create an instance if the activity is started many times. By default, singleTask mode has clearTop effect, which causes all the activities on this activity in the stack to go out of the stack. In this mode, the system calls back the onNewIntent method of the activity.

Example: Executing jump activityA - activityB - activityC - activityB, activityA and activityC are standard modes, and activityB is singleTask mode in the same APP without marker bits. In theory, the result should be AB, B at the top of the stack.

The command line query stack part of the information is as follows:

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #1:
    Task id #321
      TaskRecord{280e2df3 #321 A=com.jzf.myactivity U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.jzf.myactivity/.ActivityA }
        Hist #1: ActivityRecord{2beb98ab u0 com.jzf.myactivity/.ActivityB t321}
          Intent { flg=0x10000000 cmp=com.jzf.myactivity/.ActivityB }
          ProcessRecord{1cfbcab0 3775:com.jzf.myactivity/u0a58}
        Hist #0: ActivityRecord{1b45261d u0 com.jzf.myactivity/.ActivityA t321}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.jzf.myactivity/.ActivityA }

    Running activities (most recent first):
      TaskRecord{280e2df3 #321 A=com.jzf.myactivity U=0 sz=2}
        Run #0: ActivityRecord{2beb98ab u0 com.jzf.myactivity/.ActivityB t321}

    mResumedActivity: ActivityRecord{2beb98ab u0 com.jzf.myactivity/.ActivityB t321}

As mentioned above, Hist #1: is ActivityB, Hist #0: is ActivityA, the whole task stack has two activities, ActivityB is located at the top of the stack.
This explains that activityA - activityB - activityC - activityB, when activityC - activityB, reuses existing instances of activityB on the stack and empties the activityC that was originally located above.

singleTop: stack top multiplexing mode

If the activity of this pattern is located on the top of the task stack (the premise of reuse must satisfy this condition, oh ~), then the activity will not be recreated, and its onNewIntent method will be callback. It should be noted that the onCreate,onStart of the activity will not be called by the system because it has not changed.

singleInstance: Single instance pattern

The simple understanding is that this is an enhanced singleTask mode. In addition to all the features of singleTask, there is also a point that the activity with this mode can only be located in a single task stack. Because of the in-stack reuse feature of singleTask, no new activity will be created when the activity is subsequently started unless the task stack is destroyed.

Next, the concept of foreground task stack and background task stack should be matched with tag bits. I recommend you to read this blog first. Although I don't agree with the explanation of singleTask, the analysis of tag bits is very admirable.

http://www.cnblogs.com/xiaoQLu/archive/2012/07/17/2595294.html

The relationship between tag bits and startup modes

Firstly, we list the functions of several commonly used markers, and combine the above reading of that blog article, then we will make a summary.
1. The function of FLAG_ACTIVITY_NEW_TASK is to specify a "singleTask" startup mode for activity, and the effect is the same as that specified in the Android Menifest. XML file.
2. The function of FLAG_ACTIVITY_SINGLE_TOP is to specify a "singleTop" startup mode for activity.
3. FLAG_ACTIVITY_CLEAR_TOP has the activity of this tag bit. When it is started, all the activities above it in the same task stack will go out of the stack.

Note: The tagged start mode priority is higher than the start mode specified in the Android Menifest. XML file.

To be continued, eat first.~

Posted by renojim on Wed, 10 Apr 2019 13:30:31 -0700