Activity startup mode
1. Introduction to the Activity Launch Mode
1.1 Task stack
In Android development, Task Stack is a very important concept that is mainly used to manage the Activity and its startup mode in the application. It helps developers understand how the system manages the lifecycle and display behavior of these Activity when the user switches between different apps or jumps between different Activity inside the app. Task stack is aLast In First Out (LIFO, Last In First Out)The stack structure is used to store the Activity launched by the application.When the user launches a new Activity, the system presses it into the top of the task stack.The user can pop (destroy) the top-of-stack Activity by using the back button (back), displaying the current top-of-stack Activity.Multiple task stacks can exist in the system.The current top-of-stack Activity instance of the currently located task stack will be displayed. The current instance of the top stack Activity will be displayed.
1.2 Classification of start-up modes
In Android development, Launch Mode is how the system handles the launch of an Activity and the behavior of the task stack when managing that Activity. It determines whether an Activity creates new instances when it is launched, how it interacts with the existing task stack, and how it manages the state of the Activity during application navigation. When you launch an Activity, the system needs to decide whether to create a new instance of the Activity or reuse an already existing instance, and these behaviors are controlled by the launch modes. The standard mode, singleTop mode and singleInstancePerTask mode are for the current task stack; singleTask and singleInstance modes are for the system global. The startup modes are categorized intoStatic settingsrespond in singingdynamic settingTwo kinds.
1.2.1 standard
standard is the default startup mode of an Activity, i.e., if the current Activity does not explicitly set the startup mode, its startup mode is standard. in standard mode, every time the Activity is started, the system creates a new instance and presses it into thecurrent task stackThere is no limit to the number of Activity instances, depending on the task stack depth. This pattern is suitable for most scenarios, such as opening multiple pages in an application. The schema is shown in the following figure.
1.2.2 singleTop
When the Activity startup mode is set to singleTop, the system starts the Activity if it finds thetop of current task stack(not system global) is the Activity instance, then it will reuse the instance directly (call onNewIntent() method) without recreating it; if the top of the current task stack is not the Activity instance, then a new instance will be created regardless of whether the corresponding instance already exists in the current or other task stacks. This pattern is suitable for frequent jumps back to the current page in the same task, such as clicking from a notification to a message page. The schematic of the pattern is shown below.
1.2.3 singleTask
When the Activity startup mode is set to singleTask, the system starts the Activity if it finds theA task stack exists(system global) in the existence of the Activity instance, it will directly reuse the instance (call onNewIntent() method) without re-creation, at the same time will be located in the task stack on top of the instance of the other Activity instances will be popped out of the stack, the instance as the top of the stack of the current task stack; if all the task stack does not exist in the instance of the Activity If the Activity instance does not exist in any of the task stacks, it will be created as the top of the stack in the current task stack. The instance is created in theAll Task StacksThis pattern applies to "home" type of Activity or scenarios where there is only one instance of the Activity in the entire application. This pattern applies to the "home" class Activity, or need to ensure that the Activity in the entire application only one instance of the scenario, such as the application of the main interface, settings interface. The schematic of the pattern is shown below.
1.2.4 singleInstance
When the Activity startup mode is set to singleInstance, only one instance of the Activity is allowed to exist globally, and the Activity will occupy a task stack exclusively. When the system starts the Activity, if theAll Task StacksWhen the Activity instance does not exist in any of them, the system uses a separate task stack and creates the Activity instance in that stack, which is only used to manage the Activity instance and exists uniquely; when the Activity instance already exists in a separate task stack, the system directly reuses the instance, which is created in theAll Task Stacksin the only one that exists. This mode is suitable for lock screen page, video playback and other Activity that need to be managed independently to prevent interference. The schematic diagram of the mode is as follows.
1.2.5 singleInstancePerTask
singleInstancePerTask is a new startup mode introduced in Android API 31 (i.e. Android 12). This mode can be seen as a complement to the singleInstance and singleTask startup modes, and is intended to increase the flexibility of applications in multitasking scenarios. When the Activity startup mode is set to singleInstancePerTask, the system will start the Activity if it finds that thecurrent task stackIf an instance of the Activity exists, it will be directly reused; if not, it will be reused in thecurrent task stackThis mode is suitable for Activity that needs to run independently across multiple task stacks, but only one instance is allowed in each task stack. This mode is suitable for Activity that needs to run independently across multiple task stacks, but only one instance is allowed in each task stack, such as in multi-window operations, where each window requires a separate Activity instance.
2. Static setup startup mode
2.1 The launchMode property in the pass-through
Static settings are the settings that are set in the file, by adding them to the
Click to view code
<activity android:name=".MainActivity"
android:launchMode="singleTop">
</activity>
2.2 Static setup characteristics
- Compile-time determination: Specified at compile time and cannot be changed at runtime.
- apply globally: All startups for this Activity are handled according to the specified startup mode.
- Highly readable: At a glance in the Manifest file, which helps with maintenance.
3. Dynamically set startup mode
3.1 Passing the Flags property of an Intent
Dynamic setting refers to specifying the startup behavior of an Activity by adding specific Flags (flags) to the Intent in the code. These Flags affect the startup mode of the Activity and the management of the task stack. The example is as follows:
Click to view code
#kotlin
val intent = Intent(this, MainActivity::)
= Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
3.2 Introduction to Common Flags
- FLAG_ACTIVITY_NEW_TASK: This flag tells the system to create a new Task stack for the newly launched Activity, and if the Activity already exists on a stack, it will reuse the existing Task stack and add the Activity to the top of the stack. This flag is mandatory when starting a new Activity, especially when starting an Activity from a non-Activity context such as a Service or BroadcastReceiver.
- FLAG_ACTIVITY_SINGLE_TOP: If the started Activity is already at the top of the stack, its instance is reused instead of creating a new one. If the Activity is not at the top of the stack, the system will create a new instance and push it to the top of the stack. This flag is similar to setting singleTop in launchMode.
- FLAG_ACTIVITY_CLEAR_TOP: When a started Activity already exists on the task stack, it clears all other Activities above it, and then the system places the Activity at the top of the stack and reuses its instance. This is usually used in conjunction with FLAG_ACTIVITY_NEW_TASK.
- FLAG_ACTIVITY_CLEAR_TASK: This flag clears the entire Task stack of the target Activity. All Activity in that Task Stack is destroyed and the target Activity is started as the new stack root.
- FLAG_ACTIVITY_REORDER_TO_FRONT: If the target Activity is already on the task stack, but not the top of the stack, the system will move it to the top of the stack without destroying any other Activity on the stack or creating a new instance.
- FLAG_ACTIVITY_NO_HISTORY: This flag indicates that the started Activity will not be added to the task stack, i.e. the system will not save the state of the Activity when the user leaves it. This is often used for short-lived pages, such as login pages, advertisement pages, and so on.
- FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS: This flag prevents the started Activity from appearing in Recents. Even if the user views the list of tasks via the Multitasking button, the Activity will not appear in the list.
3.3 onNewIntent() method
When multiplexing an already existing Activity with dynamically set Flags, the system does not call the onCreate() method because no new instance is created. Instead, the system calls the onNewIntent() method. You can handle the new Intent passed to the Activity in the onNewIntent() method.
Click to view code
#Kotlin
override fun onNewIntent(intent: Intent?) {
(intent)
// Handle the new Intent
}
3.4 Dynamic setup characteristics
- High flexibility: Dynamic settings allow flexible control of Activity startup behavior based on different scenarios at application runtime. For example, you can dynamically decide whether to reuse an existing Activity instance or clean up the task stack based on user actions when receiving different Intents.
- Finer control: By combining multiple Flags, dynamic settings can provide more detailed behavioral control than static settings to meet complex application requirements.
4. Summary
In actual development, you can combine static and dynamic settings. For example, set a static startup mode for some core pages, and at the same time use Intent Flags in the code to dynamically adjust the startup behavior in specific scenarios according to the business logic.