Tasks, Back Stack, Intents, Launch Modes and Flags
Tasks, Back Stack, Intents, Launch Modes and Flags
1. What is a task?
A task is a collection of Activities that the user interacts with while completing a particular job.
Example:
Shopping task
MainActivity
|
ProductActivity
|
CartActivity
|
PaymentActivityEach task normally appears as one entry in the system Recents screen.
---
2. Activity back stack
Activities inside a task are stored in a last-in, first-out stack.
Top
PaymentActivity
CartActivity
ProductActivity
MainActivity
BottomPressing Back:
PaymentActivity removed
|
CartActivity resumedAndroid normally pushes newly started Activities onto the stack and pops the top Activity when the user presses Back.
---
3. Intents
An Intent requests an action from another Android component.
Explicit Intent
The exact component is known.
val intent = Intent(
this,
DetailsActivity::class.java
)
intent.putExtra("product_id", "P100")
startActivity(intent)Read data:
val productId =
intent.getStringExtra("product_id")Implicit Intent
The required action is described, but the component is not directly named.
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://example.com")
)
startActivity(intent)Android finds a compatible Activity using Intent filters.
---
4. Activity launch modes
Launch modes control how Activity instances are created and reused.
standard
Default launch mode.<activity
android:name=".DetailsActivity"
android:launchMode="standard" />Every launch creates a new instance.
A -> B -> B -> BUse for most normal screens.
---
singleTop
Reuses the Activity only when an instance is already at the top.
A -> B
Launch B again
A -> BThe existing Activity receives:
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
// Handle new Intent
}If B is not at the top:
A -> B -> C
Launch B
A -> B -> C -> B---
singleTask
The system attempts to reuse an existing task or Activity instance instead of creating a duplicate in the normal way.
When reused:
* The existing instance receives onNewIntent()
* Activities above it may be removed
* Its existing task can be brought to the foreground
A -> B -> C
Launch B with singleTask
A -> BUse carefully for special entry screens, authentication flows or external navigation requirements.
---
singleInstance
The Activity is isolated in its own task.
Task 1: A -> B
Task 2: SpecialActivityActivities launched by the isolated Activity generally open in another task.
This mode is rarely needed in normal applications.
---
singleInstancePerTask
Allows the Activity to be the root of its task while supporting separate instances in separate tasks when appropriate.
This is mainly useful for advanced multi-task or document-oriented behavior.
---
5. Launch mode comparison
| Mode | New instance behavior |
|---|---|
standard | Always creates a new instance |
singleTop | Reuses only if already on top |
singleTask | Reuses an existing task-level instance |
singleInstance | Isolated in its own task |
singleInstancePerTask | One root instance per task |
Most applications should use standard and control navigation through the Navigation Component rather than heavily modifying Activity launch modes.
---
6. Important Intent flags
FLAG_ACTIVITY_NEW_TASK
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)Starts the Activity in a new task or brings an appropriate existing task forward.
It is also required when starting an Activity from a non-Activity Context:
applicationContext.startActivity(
Intent(
applicationContext,
MainActivity::class.java
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)---
FLAG_ACTIVITY_CLEAR_TOP
If the Activity already exists in the current task, Activities above it are removed.A -> B -> C -> D
Open B with CLEAR_TOP
A -> B---
FLAG_ACTIVITY_SINGLE_TOP
Behaves like singleTop for that Intent launch.
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)---
FLAG_ACTIVITY_CLEAR_TASK
Clears the target task before starting the new Activity.
Usually combined with NEW_TASK.
val intent = Intent(
this,
LoginActivity::class.java
).apply {
flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(intent)Common use case:
User logs out
|
Clear authenticated screens
|
Open LoginActivity as task root---
7. Common flag combinations
Logout
Intent(this, LoginActivity::class.java).apply {
flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
}Bring existing Activity forward
Intent(this, HomeActivity::class.java).apply {
flags =
Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_SINGLE_TOP
}Start from Service or Application Context
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)---
8. Launch mode and lifecycle relationship
When an existing Activity is reused:
Existing Activity found
|
Activities above removed if required
|
onNewIntent()
|
onRestart()
|
onStart()
|
onResume()The exact callback sequence depends on the current Activity and task state.
Always handle new data inside onNewIntent() when using reusable launch modes or flags.
---
9. Common mistakes
* Using singleTask to solve ordinary navigation problems
* Forgetting to handle onNewIntent()
* Combining flags without testing Back behavior
* Creating unexpected multiple tasks
* Clearing the complete task when only one destination should be removed
* Using launch modes instead of a clear navigation architecture
---
Interview answer
> A task is a collection of Activities arranged in a last-in, first-out back stack. Intents start Activities and can carry data or describe an action. Launch modes and Intent flags control whether Android creates a new Activity, reuses an existing one, clears Activities above it or creates a new task. Most screens should use the standard launch mode, while special task behavior should be used carefully and tested with Back and Recents.