Skip to content

Instantly share code, notes, and snippets.

@arunm1407
Last active January 1, 2025 18:42
Show Gist options
  • Save arunm1407/25c19d9cd145fed6a66dac98d32884bd to your computer and use it in GitHub Desktop.
Save arunm1407/25c19d9cd145fed6a66dac98d32884bd to your computer and use it in GitHub Desktop.
Overview
Standard: A new instance is created every time.
SingleTop: Reuses the activity if it's already at the top of the stack.
SingleTask: Clears everything above the existing activity instance in the stack.
SingleInstancePerTask: Creates a new task for each instance, ensuring independence.
Task
A task consists of a collection of activities that users engage with while performing specific actions within your app.
Activities within a task are arranged in a structure called the backstack.
Backstack maintains activities in the order in which they are launched.
Launch modes allow you to specify how a new activity instance is linked to the existing task.
It can be defined in 2 ways:
Using Manifest.xml file
Using intent filters
Types of Launch Modes
standard
singleTop
singleInstance
singleTask
singleInstancePerTask
standard
default launch modes
creates a new instance everytime in the task.
The task with activities:
A -> B -> C -> D
Start new activity E:
A -> B -> C -> D -> E
Again start activity B:
A -> B -> C -> D -> E -> B
singleTop
a new instance is created if the activity is not at the top of the stack
onNewIntent() is called if activity is at the top
The task with activities:
A -> B -> C -> D
Start new activity E:
A -> B -> C -> D -> E
Again start activity E:
A -> B -> C -> D -> E
onNewIntent() of Activity E is called
Again start activity C:
A -> B -> C -> D -> E -> C
singleTask
activity’s instance is created once in a lifetime
activities on top are removed and onNewIntent() is called
The task with activities:
A -> B -> C -> D
Start new activity E:
A -> B -> C -> D -> E
Again start activity C:
A -> B -> C
onNewIntent() of Activity C is called. Activity D & E is removed.
singleInstance
activity is created in new task
no other activities can be added to the task
if activity is already present then it is reused
single activity instance will be used in whole system.
The task with activities:
A -> B -> C -> D
Start new activity E:
Task 1 : A -> B -> C -> D
Task 2 : E
Again start activity E:
onNewIntent() of Activity E is called. No new task will be created
singleInstancePerTask
one instance of an activity per task
allows other activities to exist in same task
onNewIntent() is called if activity is called in same task
The task with activities:
A -> B -> C -> D
Start new activity E:
Task 1 : A -> B -> C -> D
Task 2 : E
Again start activity E from Task 2:
Task 1 : A -> B -> C -> D
Task 2 : E
onNewIntent() will be called on Activity E in Task 2.
start activity E in new task:
Task 1 : A -> B -> C -> D
Task 2 : E
Task 3 : E
Ways to define launch modes
Using Manifest file
standard
<activity android:name=".MainActivity" android:launchMode="standard" />
singleTop
<activity android:name=".MainActivity" android:launchMode="singleTop"/>
singleTask
<activity android:name=".MainActivity" android:launchMode="singleTask"/>
singleInstance
<activity android:name=".MainActivity" android:launchMode="singleInstance" />
singleInstancePerTask
<activity android:name=".MainActivity" android:launchMode="singleInstancePerTask" />
Using intent flags
standard
val intent = Intent(this, MyActivity::class.java)
startActivity(inte
*No intent flag is required
singleTop
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
singleTask
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
singleInstance
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
startActivity(intent)
singleInstancePerTask
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
startActivity(intent)
Jetpack Compose
standard
navController.navigate("destination")
singleTop
navController.navigate("destination") { launchSingleTop = true }
singleTask
navController.navigate("destination") {
popUpTo("destination") { inclusive = false } launchSingleTop = true }
singleInstance / singleInstancePerTask
Not directly supported as compose focuses on single activity using NavHost and NavContoller API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment