Deep Dive Into Activity Launch Modes in Android.

Activity Launch Modes
In Android, Activity Launch Modes determine how new instances of an activity are created and how they interact with existing activities. The four main launch modes are:
- Standard (default)
- SingleTop
- SingleTask
- SingleInstance
Each launch mode controls the behavior of activities when they are launched and influences the activity lifecycle differently.
To understand the interaction between the activity lifecycle and launch modes, let’s break down the behavior of each mode along with a flowchart-like explanation.
Activity Lifecycle and Launch Modes
Standard Launch Mode (Default)
Every time you launch an activity, a new instance of the activity is created and pushed onto the activity stack.
Behavior: No special restrictions on activity creation.
Lifecycle Flow: When the activity is launched, it goes through onCreate()
, onStart()
, onResume()
, and so on.
Example: Launching the same activity from multiple places in your app will create multiple instances of the activity.
- Activity 1 → Activity 2 (New instance created)
- Activity 2 → Activity 3 (New instance created)
2. SingleTop Launch Mode
If an activity is already at the top of the activity stack, no new instance is created. Instead, the existing instance is brought to the foreground, and onNewIntent()
is called to handle the new intent.
Behavior: If the activity is not at the top, it will behave like the standard mode and create a new instance.
Lifecycle Flow: If the activity is already at the top, it skips onCreate()
and goes directly to onNewIntent()
.
Example: If you launch the same activity while it is already in the foreground, no new instance is created.
- Activity 1 → Activity 2 (New instance created)
- Activity 2 → Activity 3 (New instance created)
- Activity 3 → Activity 2 (Existing instance brought to front)
3. SingleTask Launch Mode
A new task will be created for the activity. If the activity is already in the task (but not at the top), all activities above it in the stack will be removed, and the activity will be brought to the top.
Behavior: There will be only one instance of the activity in the task, and it is launched in a separate task.
Lifecycle Flow: The activity is launched in a new task if it’s not already in the task, and any existing activities above it are cleared.
Example: Launching a settings screen might use singleTask to ensure there’s only one instance of the settings activity.
Flowchart Example:
- Activity 1 → Activity 2 (New instance created)
- Activity 2 → Activity 3 (New instance created)
- Activity 3 → Activity 2 (Activity 2 brought to the top and others removed)
4. SingleInstance Launch Mode
The activity is always the only activity in its task. When a new instance of the activity is launched, it will be placed in a new task (if the activity is not already running), ensuring there is only one instance in the entire system.
Behavior: The activity is launched in its own task, and there will never be more than one instance of it in the system.
Lifecycle Flow: A new task is always created, and if the activity is already running, the current instance is brought to the foreground with onNewIntent().
Example: An app launcher screen might use singleInstance to ensure only one instance of the launcher activity is ever created.
- Activity 1 → Activity 2 (New task created)
- Activity 2 → Activity 2 (onNewIntent() called)
Flowchart of Activity Lifecycle with Launch Modes
Here’s how the launch modes interact with the activity lifecycle in different scenarios:
Scenario 1: Standard Mode
When an activity is launched, it always creates a new instance and pushes it onto the activity stack.
- Activity 1 → Activity 2 (new instance created)
- Activity 2 → Activity 3 (new instance created)
- Activity 3 → Activity 1 (new instance created)
Scenario 2: SingleTop Mode
If the activity is at the top of the stack, no new instance is created, and onNewIntent() is called.
- Activity 1 → Activity 2 (new instance created)
- Activity 2 → Activity 1 (onNewIntent() called, no new instance)
Scenario 3: SingleTask Mode
If the activity is not in the stack, a new task is created, and all activities above it in the stack are cleared.
- Activity 1 → Activity 2 (new instance created)
- Activity 2 → Activity 3 (onNewIntent() called, no new instance)
- AActivity 2 (top) → Activity 3 (Activity 3 is cleared, Activity 2 brought to top)
Scenario 4: SingleInstance Mode
The activity is always the only one in its task. If it is already running, onNewIntent() is called. If not, it is launched in a new task.
- Activity 1 → Activity 2 (new task created, only one instance of Activity 2)
- Activity 2 → Activity 2 (onNewIntent() called)
Conclusion
Understanding the activity launch modes in Android is crucial for managing the activity stack and controlling how activities behave when launched from different places in the app. Each launch mode offers a different level of control over how activities are created and how they interact with each other, which can optimize the app’s memory and user flow. Standard: Default behavior, new activity instances are created every time. SingleTop: No new instance if the activity is already on top. SingleTask: New task is created; all activities above are cleared. SingleInstance: A new task is created with only one instance of the activity. By selecting the appropriate launch mode based on your app’s flow, you can manage resources more effectively and control the activity lifecycle to ensure better performance and user experience.