Launch Mode Explained in Android

Let’s jump directly to the point.
What is launch mode?

Harjot Singh
5 min readNov 1, 2022
Photo by Andy Hermawan on Unsplash

The attribute android:launchMode is defined as an instruction on how the activity should be launched. It is defined in manifest of the activity.

Syntax:-

<activity
android:name=".SomeActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"

</activity>

Not so clear..right?
Well, let’s get into the good stuff…

There are 5 types of launch mode ->

  • standard
  • singleTop
  • singleTask
  • singleInstance
  • singleInstancePerTask

Standard

android:launchMode = “standard”

This is by default the launch mode of activity i.e. even if no mode is set for the activity, by default it is in standard mode.

Every time, there’s a new intent for the activity…a new instance is created and it is added to the back stack even if the instance is already present.

Let’s look at an example for this:
User starts an application. A activity is called; from A user goes to activity B; from B to activity C and from C again to activity A. The order is as follows:
A -> B -> C -> A
This is the order in which activities are pushed to the stack.

NOTE: instance of Activity A is created again.

Single Top

android:launchMode = “singleTop”

Let’s see this flow:
A -> B -> C -> D

From D, activity B is started. Hence, the flow becomes:
A -> B -> C -> D -> B
Now, say again instance of activity B is created:
A -> B -> C -> D -> B -> B
You thought this would be the order in stack? Well no.
The order will be
A -> B -> C -> D -> B

singleTop launch mode is just like standard mode except for one difference; no new instance of the activity will be created if it’s already at the top of the stack.

Before moving ahead, make sure you know what tasks are? If you are unaware about it, please read the following article to get the idea

For now, I’ll give a short hint to what a task is…A task is defined as stack of activities.

Single Task

android:launchMode = “singleTask”

Every time, an activity defined with this launch mode basically means the activity will be started in a new task with it being the root of that task stack (and other activities are pushed on top of it) or locates the activity on the existing task with same affinity.

Case I

Let’s understand the line before the or…
This happens if within the manifest we define another attribute : documentLaunchMode. Basically, this attribute decides whether to create a new task or not.

Not so clear? To get in depth of what I’m saying …you can follow this blog

Back to Case I

Follow the old example:
A -> B -> C -> D
Here activity B has singleTask launchMode
So, it actually becomes:
A (Task — 1)
B -> C -> D (Task — 2) with B as the root of the activity.

Say from activity D, activity B is created. The flow would become:
A (Task — 1)
B (Task — 2)
Boom!

NOTE: Only one instance of the activity can exist at a time.
Also note, rather than creating a new instance, the system routes the intent to existing instance.
A (Task — 1)
B (Task — 2)

Case II

Let’s understand the line after or …

We follow this example:
A -> B -> C -> D
Activity B has the singleTask launchMode.

Now, from Activity D …we call Activity B; the flow becomes:
A -> B

Hitting back button, would take user back to activity A.

Single Instance

android:launchMode = “singleInstance”

It is similar to singleTask except that the system doesn’t launch any other activity into the task holding the instance.
When activities with this launchMode are started, a new task is created every time and in that separate task the activity instance is the root and only member of the task stack.

This is the flow:
A -> B -> C -> D

Here, say activity E has singleInstance as the launchMode. When, instance of E is created…the flow changes to ->
A -> B -> C -> D ( Task — 1)
E (Task — 2)

Here Task-2 comes to foreground and Task-1 is in the background

From E, new instance F is created. The flow becomes ->
A -> B -> C -> D -> F( Task — 1)
E (Task — 2)

Here Task-1 comes to foreground and Task-2 is in the background

Again, if instance of E is created…the onNewIntent() will route back to the existing instance and Task-2 will come to foreground.

Say, Activity G also has singleInstance as the launchMode and Activity F launches Activity G. Now, the flow becomes:
A -> B -> C -> D -> F (Task — 1)
E (Task — 2)
G (Task — 3)

Single Instance Per Task

android:launchMode = “singleInstancePerTask”

You might have guessed by now.
This is similar to what singleInstance is except for the fact that the activity with this launchMode can be instantiated multiple times in different tasks when either documentLaunchMode is set to always or FLAG_ACTIVITY_MULTIPLE_TASK/ FLAG_ACTIVITY_NEW_DOCUMENT is set as the Intent flag.

If you’ve stayed this far please make sure to give a clap and hit the follow button.
Thank you and happy coding! 😄😄

Want to learn about coroutines?
Feel free to check out the blog ->

--

--

Harjot Singh

Android Developer @HealhifyMe I lose my handwritten notes often...so now making sure I can find my notes easily :p