Launch Modes in Android
Have you ever wondered how your Android apps smoothly switch from one screen to another? It's like a well-choreographed dance, and Intents are the cues that make it happen. But what about the behind-the-scenes magic that governs how activities behave within the app? That's where launch modes come into play. They're like rules that dictate how activities are opened and managed in an app. In this article, we will explore launch modes in Android, shedding light on how they influence the behaviour of your app's activities.
But before moving ahead let us understand the following terms:
Task - A task is a collection of activities users interact with when trying to do something in your app. An app can have multiple tasks. For example, if you're using a browser and open a link in a new tab, that link opens in a new task. Each task operates independently.
Backstack - Each task has a backstack. This is a stack of activities in the order in which they were opened. When you press the back button, you go to the previous activity in the stack.
Launching Activities - When you start a new activity, it's usually added to the current task's stack. However, you can use launch modes to control how the new activity interacts with the existing task.
Affinity and Task Affinity - Activities can be assigned an "affinity", which is like a label. Activities with the same affinity are part of the same task. However, activities with different affinities are part of different tasks.
What are Launch Modes?
Launch modes are like a set of rules that tell Android how to start and manage activities. They decide if a new activity should be created or if an existing one should be used. Launch modes let you define how a new instance of an activity is associated with the current task. You can define launch modes in two ways, described in the sections that follow:
-
When you declare an activity in your manifest file, you can specify how the activity associates with tasks when it starts.
<activity android:name=”.MainActivity” android:launchMode=”singleTop” />
-
When you call
startActivity()
, you can include a flag in theIntent
that declares how (or whether) the new activity is associated with the current task.
val intent = Intent(this, NewActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
startActivity(intent)
Different Launch Modes :
1. Standard -
When no launch mode is set on your activity, it will use standard mode by default. Each time, it creates a new instance of activity, regardless of whether it already exists.
- Suppose we have four activities in order -
A -> B -> C -> D
- Start Activity B which has a standard launch mode -
A -> B -> C -> D -> B
As we can see, a new instance of B has been created, and we now have two instances of B in the task.
2. Single Top -
In this launch mode, when an activity is at the top of the task and you create its instance again, a new instance will not be created. Instead, onNewIntent() will receive updated data through Intent data. And, if the activity is not at the top, a new instance will be added.
- Suppose we have four activities in order -
A -> B -> C -> D
Start Activity C.
As C is not on top, a new instance of C will be created.
A -> B -> C -> D -> C
Start Activity C again which has a single top launch mode
As C is on the top, a new instance of C will be not created. Instead, onNewIntent() method will be called of Activity C.
A -> B -> C -> D -> C (onNewIntent())
3. Single Task -
In this launch mode, if the task does not contain the activity, a new instance is generated. If the activity already exists then onNewIntent() is invoked rather than creating a new instance. Meanwhile, all of the other activities on top of it are destroyed.
Single Task has two different cases depending on the taskAffinity. ( Single Task with different taskAffinity and Single Task with same taskAffinity. )
By default, the taskAffinity of an activity is set to the package name of the app. This means that activities within the same app will have the same affinity, and they will be part of the same task.
Case 1 - Single Task with Different taskAffinity
In this case, a new task is created for the activity which is launched with Single Task with different taskAffinity mode. Let's see the example -
Suppose we have activity A in a task and now we launch Activity B having single task launch mode.
Task 1 - A
Task 2 - B*
// Task 2 is in foreground
Now we launch Activity C then after launching Activity D, it will be
(new activity will launch with the affinity of root activity)
Task 1 - A
Task 2 - B* -> C -> D
// Task 2 is in foreground
- Now we switched to Task 1 and then launched Activity C after launching Activity D, it will be -
Task 1 - A -> C -> D
Task 2 - B* -> C -> D
// Task 1 is in foreground
Now what happens if we launch Activity B (which has a single task launch mode) from Activity D ( one which is in Task 1)?
Since B is a single-task activity, Activity C and Activity D of Task 2 will pop up and Activity B will be launched using the existing instance.
Task 1 - A -> C -> D
Task 2 - B*
// Task 2 is in foreground
Case 2 - Single Task with the same taskAffinity
In this case, a new task is not created for the activity which is launched with Single Task with same taskAffinity mode. Let's see the example -
Suppose we have activity A in a task and now we launch Activity B having single task launch mode.
A -> B
- Now we launch Activity C then after launching Activity D, it will be
A -> B* -> C -> D
- Now when we launch B, Activity C and Activity D will pop up and Activity B will be launched using the existing instance.
A -> B*
4. Single Instance -
In this launch mode, if the task does not contain the activity, a new instance of activity is created in a new task. The activity is always the single and only member of its task. Any activities started by this one open in a separate task.
- Suppose we have Activity A in a task and now we launch Activity B which has a single instance launch mode
Task 1 - A
Task 2 - B*
// Task 2 is in foreground
- Now we open Activity C, it will be
Task 1 - A -> C
Task 2 - B*
// Task 1 is in foreground
- Now we open Activity B, the same instance of B, the
onNewIntent()
the method will be called.
Task 1 - A -> C
Task 2 - B*
// Task 2 is in foreground
What if affinity is associated with a single instance too just like a single task?
With the same task affinity - It will only show one task which is visible and will not show any previous task on the user's recent screen.
With different task affinity - This will show the number of tasks which have been available on the user's recent screen. The benefit is that we can switch between different tasks through a recent screen.
Thank you for taking the time to explore launch mode with us. We hope this article has been insightful. If you have any questions or would like to share your thoughts, please feel free to comment. Happy Coding!
Subscribe to my newsletter
Read articles from Priyanshu Borole directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by