Android manages activities through the task stack. The stack is an advanced data structure, and the Activity displayed in the window is on the top of the stack. Only the Activity on the top of the stack is visible, when we enter a new Activity, The Activity will be on the stack and on top, and when we exit the Activity with the Back key, it will be off the stack.

The task stack is not unique; normally (in Standard mode), an Activity goes to the same stack as the Activity that started it.

The startup mode of an Activity

There are four startup modes for an Activity. They are Standard, singleTop, singleTask, and singleInstance.

Standard Standard mode

Standard mode is the system’s default startup mode, which creates a new instance every time an Activity is started, regardless of whether the instance already exists, and they go through the typical Activity lifecycle, onCreate, OnStart and onResume (whether it already exists or not).

SingleTop Stack top multiplexing mode

In top-of-stack reuse mode, if the Activity to be started is at the top of the task stack, the Activity is not recreated and does not go through its normal life cycle. OnCreate and onStart are not called, but the onNewIntent method is called. The parameter to this method gives us the new request information, the Intent. If the Activity to be launched already exists but is not on the top of the stack, the Activity is recreated.

SingleTask Intra-stack reuse mode

The in-stack reuse mode is a singleton mode in which the Activity is not created as long as it exists on the stack to which it is to be launched, and the system moves the Activity to the top of the stack. If the Activity does not exist on the stack, the Activity will be created and placed on the stack.

Note that if this Activity exists on the stack, the system moves it to the top of the stack by taking all the activities on it off the stack, clearTop.

For example, the current task stack is ADBC, and D is about to enter this stack. Since D exists in this stack, it will be moved to the top of the stack. At this time, the situation in the stack is AD.

SingleInstance singleInstance mode

The single instance pattern can be thought of as an enhanced version of the singleTask (in-stack reuse pattern). In addition to having all the features of singleTask, it has the other, perhaps confusing, feature that an Activity with this pattern can only be located in a singleTask stack. In general, when an Activity is created in singleTask mode, the system creates a separate task stack for it. If the Activity is destroyed, its task stack is also destroyed.

How do I set the launch mode of an Activity

Proper use of the launch mode of an Activity can improve the performance of a product, so how to set the design mode of an Activity?

Set the launch mode for your Activity using AndroidMainfest
<activity
    android:name=".activity.SplashActivity"          
    android:configChanges="screenLayout"
    android:launchMode="singleTask"
    android:label="@string/app_name" />
Copy the code
The Intent flag is set to specify the launch mode for the Activity
val intent = Intent()
intent.setClass(this, MainActivity::class.java)        
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)        
startActivity(intent)
Copy the code

Although both methods can set a startup mode for an Activity, the two methods have different priorities. The second method takes precedence over the first method. When an Activity is set to start by both methods, the second method takes precedence.