Android’s four startup modes Standard, SingleTask, SingleTop, SingleInstance and their functions are not described here, this belongs to the basis of the foundation, I think we already know. The only thing that can be problematic here is singleInstance, so this article uses concrete code examples to demonstrate the use of the Task stack when activities in different launch modes jump to each other.

Here we create the following activities, which, by definition, start in the same mode as the file name.

<activity android:name=".SingleInstanceActivity2"
          android:launchMode="singleInstance"/>

<activity android:name=".SingleTopActivity"
          android:launchMode="singleTop"/>

<activity android:name=".SingleTaskActivity"
          android:launchMode="singleTask" />

<activity android:name=".SingleInstanceActivity"
          android:launchMode="singleInstance" />

<activity android:name=".StandardActivity"
          android:launchMode="standard" />

<activity android:name=".MainActivity"
          android:launchMode="standard">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Copy the code

Scene 1:

What happens when MainActivity starts SingleInstanceActivity and then starts MainActivity again in SingleInstanceActivity? Click back and go back to where?

View the task stack

From the CMD command line or Terminal in Android Studio type the following command:

adb shell dumpsys activity
Copy the code

Click the back key to exit the stack:

We found that the order is MainActivity->MainActivity->SingleInstanceActivity. We can see the pattern of clearing one stack before clearing the second.

Scene 2:

MainActivity starts SingleInstanceActivity and then starts SingleTaskActivity again in SingleInstanceActivity. Click back and go back to where?

View the task stack

Click the back key to exit the stack:

We found that the order is SingleTaskActivity->MainActivity->SingleInstanceActivity. We can see the pattern of clearing one stack before clearing the second.

Scenario 3:

MainActivity starts SingleInstanceActivity and then starts SingleTopActivity again in SingleInstanceActivity. Click back and go back to where?

View the task stack

Click the back key to exit the stack:

We find that the order is SingleTopActivity->MainActivity->SingleInstanceActivity. We can see the pattern of clearing one stack before clearing the second.

Scene 4:

MainActivity starts SingleInstanceActivity, and then SingleInstanceActivity2 starts again in SingleInstanceActivity, What happens when you start StandardActivity again in SingleInstanceActivity2? Click back and go back to where?

View the task stack

Click the back key to exit the stack:

We found that the order is StandardActivity->MainActivity->SingleInstanceActivity2->SingleInstanceActivity. We can see the pattern, which is to clear one stack and then clear the second stack, and then clear the third stack.

Conclusion:

1.Standard, SingleTask, and SingleTop activities are placed on the same Task stack, and each SingleInstance is placed on a separate stack

3. Each time a different Activity is created in SingleInstance mode, a new Task stack is created to hold the Activity.

3. Check the current stack when unstacking, and then unstack the second stack after the current stack is unstacked.