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
Understanding how launch modes interact with the Android activity lifecycle is crucial for building efficient applications.
Standard Launch Mode
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)
SingleTop Launch Mode
SingleTop mode lets an activity reuse itself if it's already on top of the stack, otherwise creates a new instance.
Behavior: If the activity is already at the top of the stack, onNewIntent()
is called instead of creating a new instance.
- Activity 1 → Activity 2 (New instance created)
- Activity 2 → Activity 2 (Existing instance,
onNewIntent()
called)
SingleTask Launch Mode
SingleTask ensures only one instance of the activity exists in the task stack.
Behavior: If the activity exists anywhere in the stack, all activities above it are destroyed, and onNewIntent()
is called.
SingleInstance Launch Mode
SingleInstance creates the activity in a separate task and ensures it's the only activity in that task.
Behavior: The activity runs in its own task, isolated from other activities.
Comparison Table
Launch Mode | Instance Creation | Use Case |
---|---|---|
Standard | Always creates new | Default behavior |
SingleTop | Reuses if on top | Search activities |
SingleTask | One per task | Home screen |
SingleInstance | One in separate task | Launcher activities |
Best Practices
- Use Standard mode for most activities
- Use SingleTop for search or filter activities
- Use SingleTask for main/home activities
- Use SingleInstance sparingly, only for special cases
Frequently Asked Questions
<activity android:name=".YourActivity" android:launchMode="singleTop" />