We know that by default, whenever we start the same Activity more than once, multiple instances are created and added to the task stack one by one. When we click the Back key, we see that the Activity falls back and forth. Isn’t it silly to create multiple instances of the same Activity multiple times when you start it? So Android provides launchMode to modify the system’s default behavior.

There are four startup modes that all Android developers are familiar with: Standard, singleTop, singleTask, and singleInstance

Before we can understand these four startup modes, we need to understand the taskAffinity concept

TaskAffinity task attraction

  • TaskAffinity refers to the Activity’s attribution, the Activity’s attachment to the Task, that is, the Task to which the Activity belongs. Typically, within the same application, the activities that are started are all in the same Task, and they live out their lives in that Task.
  • Each Activity has a taskAffinity attribute, which identifies the Task it wants to enter. If an Activity does not explicitly specify taskAffinity, then this property is equal to the taskAffinity specified by the Application; if the Application does not specify taskAffinity, then the taskAffinity value is equal to the Application package name.
  • You can specify a separate affinity for an Activity by adding the taskAffinity attribute to the element. The value of this attribute is a string, which can be specified as any string, but must contain at least one “. Otherwise, an error will be reported.
  • TaskAffinity is configured in the Manifest:
<activity
        android:name="com.test.TestActivity"
        android:configChanges="orientation|keyboard|keyboardHidden"
        android:exported="true"
        android:taskAffinity="com.test.TestActivity"
        android:screenOrientation="portrait"/>
Copy the code

Meaning of the four startup modes

Standard Standard mode

This is also the default mode of the system. Each time an Activity is started, a new instance is repeatedly created, regardless of whether the instance already exists. In this mode, whoever started the Activity runs in the same stack as the Activity that started it. If an Activity and its Application do not specify taskAffinity, its taskAffinity will default to the package name.

If you use ApplicationContext to start an Activity in Standard mode, the following error occurs

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. This error may be seen by many students because standard mode activities default to the task stack that starts their Activity. It’s not an Activity Context, it doesn’t have a task stack, so you crash. The solution to this problem is to specify the FLAG_ACTIVITY_NEW_TASK flag bit to start the Activity and create a new task stack for it when it starts.

SingleTop stack top reuse mode

In this mode, if a new Activity is already at the top of the stack, the Activity is not recreated, its onNewIntent methods are called back, and onCreate and onStart are not called by the system.

SingleTask stack reuse pattern

In this mode, as long as the Activity exists in a task stack, launching the Activity multiple times does not create a new instance and, like singleTop, calls back to onNewIntent. The system processing logic for this process is that when A singleTask Activity request is started, the system first looks for the presence of A desired task stack (specified by taskAffinity).

  • If not, create a task stack and place the Activity at the top.
  • If the desired task stack already exists, there are two scenarios: if the Activity does not exist on the stack, create an instance of the Activity and push it to the top; If the Activity is present on the stack, it is pushed to the top of the stack and all activities on it are removed from the stack. Therefore, singleTask mode has clearTop effect by default.

SingleInstance Single-instance mode

This is an enhanced singleTask mode, which has all the features of singleTask. And there is an added bonus: he can only stand alone on a task stack. The system will start a new stack structure for it, place Acitvity in the new stack structure, and ensure that no other Activity instances enter.

Use the Flags of the Intent to specify the launch mode

FLAG_ACTIVITY_NEW_TASK alone is not equivalent to the launch mode singleTask, it simply represents the task stack push required to find an Activity (as specified by taskAffinity)

FLAG_ACTIVITY_NEW_TASK’s relationship to singleTask

Even in FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP, In androidmanifest.xml, setting the activity startup mode to standard or singleTask is different

  • In standard mode, if an instance of the activity already exists on the required stack, the instance and other activities above it are removed from the stack, anda new instance of the activity is added to the stack.
  • When the startup mode is singleTask, if an instance of the activity already exists on the required stack, the system calls the onNewIntent() method of that instance and pushes only those activities above that instance off the stack.
  • If the required stack does not have an instance of the activity, the new activity instance is added directly to the stack, regardless of whether the startup mode is Standard or singleTask.
  • Androidmanifest.xml sets the startup mode of an activity to singleTask. Are both FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP or only FLAG_ACTIVITY_NEW_TASK effect is the same, The default singleTask mode is FLAG_ACTIVITY_CLEAR_TOP.

FLAG_ACTIVITY_SINGLE_TOP Specifies the “singleTop” startup mode for activities.

I’m sure there are a lot of details that some experienced Android programmers haven’t noticed before. Write it down here so we can look it up.