Have you ever clicked on a notification and seen the same screen open three times in the back stack? That’s not a bug — it’s a mismanaged Launch Mode.
In this article, we’ll break down how Android handles Activities, Tasks, Launch Modes, and Task Affinity, and why understanding these concepts is essential for building smooth and predictable navigation in your app.
How Android Manages Screens and Navigation
In Android, every screen is an Activity. But Android doesn't just open screens — it stacks them within a Task (Back Stack). This stack controls how users move between screens using the Back button.
Task Affinity (The Rule Behind the Task)
Every Android Activity has something called Task Affinity. Task Affinity determines which Task an Activity prefers to reside in.
By default, all Activities in the same app have the same affinity, so they reside in the same back stack (task). But you can change this in the Manifest to make an Activity open in a different task.
This becomes especially important when used with launch modes like SingleTask or SingleInstance, because Android uses task affinity to determine:
- Whether to reuse an existing task
- Whether to create a new task
- Where the activity will appear in the Recents screen
If you don't understand Task Affinity, launch modes like SingleTask can behave strangely.
What do Launch Modes determine?
Launch modes control:
- Whether a new Activity is created or an old one is reused
- How Activities are added to the back stack
- What happens when the Back button is pressed
Understanding the 4 Android Launch Modes
Android has four types of Launch Modes, and each one changes how Activities behave inside the task.
1. Standard (Default Launch Mode)
Standard is the default launch mode in Android. Every time an activity is launched, Android creates a new instance of it — even if the same activity already exists in the back stack.
The Stack: A → B → B → B (If you opened B three times).
Each time B is opened, a new instance is added.
Best For: Simple screens like a settings page or a form where you might actually want multiple instances.
2. SingleTop
In SingleTop, Android checks if the activity is already at the top of the back stack.
If it's already at the top, a new instance is not created. Instead, the existing instance receives the new intent via onNewIntent().
If the activity is not at the top, a new instance is created in the normal way.
The Stack: A → B → (Open B again) → A → B
Best For: Search result pages, screens opened from notifications and screens that open repeatedly with new data.
3. SingleTask
SingleTask ensures that there is only one instance of the activity in the entire task.
If the activity already exists anywhere in the task, Android:
- Brings that instance to the foreground
- Removes all activities above it
The Stack: A → B → C → D (Open B again) Final Stack: → A → B (C and D are destroyed).
Best For: Your Main Dashboard or Home Screen. You never want three copies of your "Home" screen sitting in the background.
4. SingleInstance
In SingleInstance mode, the activity always runs alone in its own separate task.
No other activity can be included in that task. And if this activity launches another activity, it will open in a different task.
Example:
Task 1: A → B, Launch D (SingleInstance)
Task 1: A → B
Task 2: D
Even if D opens another activity, it will not be added to Task 2.
The Behaviour: It’s like having a mini-app inside your app that lives in its own world.
Best For: Specialist screens like an Incoming Call screen, an Alarm clock or audio/video player screens. You want these to be isolated from the rest of the app's navigation.
How Do We Set Launch Modes in Android?
Inside your AndroidManifest.xml, you can define how an Activity should behave every time it opens.
<activity
android:name=".PlayerActivity"
android:launchMode="singleInstance"
/>
Or programmatically using Intent flags (Dynamic Way)
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
startActivity(intent)
Problems Caused by Incorrect Launch Modes
When launch modes are not handled correctly, your app can exhibit these problems:
- The same screen opening multiple times
- Confusing back navigation
- The app consuming excessive memory
- A clunky and broken user experience
Conclusion
Once you understand how Android handles tasks, the back stack, launch modes, and task affinity, your app’s navigation will stop feeling random and start feeling reliable.
Understanding these four launch modes and task affinity helps you avoid duplicate screens, maintain clean back stack behavior, and provide a smooth user experience.