Android Activity Launch Modes

Himmat Sawant
6 min readDec 22, 2020

--

There are 4 launch mode in Android

  1. standard
  2. SingleTop
  3. SingleTask
  4. SingleInstance

These launch mode define how activity get started or launched.
Task is the collection of activities that user interact with when dealing with app.
When activity get launched new instance of activity is created and that instance is pushed to activity stack. This activity stack is also called task and task can contains one or more
instances.

You can specify in which mode activity gonna be launch is declared in AndroidManifest file.

<activity
android:name=”.MainActivity”
android:label=”standard launchMode”
android:launchMode=”standard”>

If there is 4 activities A,B,C and D. And you launch Activity A after that you launch Activity B then Activity C and last launched Activity D stack will be

  1. Standard :

This is the default launch mode in android for activities. When activity get launched with “Standard” launchMode new instance of activity get created
in the task from which it was started.
Every time activity get launched with launchMode “Standard” new instance of activity get created and pushed to stack. So multiple instances of activity can be created and added to same and different task.

Example :-

You have 4 Activities Activity A, Activity B, Activity C and Activity D. Activity B has launchMode as “Standard”.

Case 1 : If you launch Activity A then you launch Activity B ( B has launchMode “Standard”). Your stack will be

A -> B

After that you launch B again stack will be

A -> B -> B

Case 2: If you launch Activity A then Activity B then Activity C and at last Activity D stack will be

A -> B -> C -> D

Now you launch Activity B again (B has launchMode “Standard”) then stack will be

A -> B -> C -> D -> B

With launchMode “Standard” everytime you launch activity new instance of that activity get created and push to stack.

2. SingleTop :

If activity get launched with launchMode “SingleTop” means if activity instance is on the top of the stack then don’t create new instance use old or previously created instance
and if instance of activity is not on top of the stack then only create new instance of that activity ( For Activity that launched with launchMode “SingleTop”).

In this launchMode you can also create multiple instances of activity if and only if that instance is not on the top of the stack.

Example

You have 4 Activities Activity A, Activity B, Activity C and Activity D. Activity B has launchMode as “SingleTop”.

Case 1: If you launch Activity A then Activity B after that you launch Activity C but with launchMode “SingleTop” then stack will be

A -> B -> C

If you try to launch Activity C again which has launchMode “SingleTop” then stack will be

A -> B -> C

stack will remains same no new instance of Activity C get created because Activity C is on the top of the stack that’s why old instance get used again but
new not get created.

Case 2: If you launch Activity A then Activity B after that you launch Activity C but with launchMode “SingleTop” at the end you launch Activity D then stack will be

A -> B -> C -> D

After that if you again launch Activity C with launchMode “SingleTop” then stack will be

A -> B -> C -> D -> C

Here duplicate new instance of Activity C get created because in this case Activity C is not on the top of stack. So new instance get created.

Before going further lets understand first “What is taskAffinity ?”

TaskAffinity : If you use taskAffinity attribute in your AndroidManifest file at time of activity registration then it create separate task for that particular activity that launch with singleTask.

Every activity in AndroidManifest has by default same affinity so all activities is pushed to same task. Your Package name is the default affinity for all activities in application.

To change affinity for particular activity you just need to use taskAffinity attribute and give package name different from default one.

<activity name=”XXXActivity”
launchMode=”singleTask”
taskAffinity=”com.xxx.xxx.sa”>
</activity>

Lets continue with LaunchMode…

3. SingleTask :

In this SingleTask launchMode new task/stack will always created and new instance will be pushed to stack as root one.
When activity get launched with launchMode “SingleTask” new instance get created in new task.

Example

You have 4 Activities Activity A, Activity B, Activity C and Activity D. Activity B has launchMode as “SingleTask”.

A) SingleTask without taskAffinity (means all activities get pushed to same task)

If you launch Activity A then you launch Activity B but with launchMode “SingleTask” after that you launch Activity C and then Activity D then stack will be

A -> B -> C -> D

Now if you launch Activity C again then stack will be

A -> B

Activity C and D get destroyed/removed from stack and Activity B which has launchMode “SingleTask” become new root.


In this case (without affinity) all stuff happening in single task only.


B) SingleTask with taskAffinity (means more than on task get created)

<activity name=”BActivity”
launchMode=”singleTask”
taskAffinity=”com.xxx.xxx.sa”>
</activity>

If you launch Activity A then you launch Activity B but with launchMode “SingleTask” then stack will be

a) A
b) B

Two new stack will be created Activity A get launched in first task (as usual) then Activity B get launched and pushed to second task because
you declared separate affinity in AndroidManifest for Activity B.

After that if you launch Activity C and Activity D the stack will be

a) A
b) B -> C -> D

If you pay attention to point(b) which is the second task Activity C and D get launched in task second because task second is on foreground.
If you press recent button and bring task one on foreground and then launch Activity C and D then stack will be
(Note :- If you press recent button in this case you see two task in recent list).

a) A -> C -> D
b) B

In below case
a) A
b) B -> C -> D

if task second (b) is on foreground and you try to launch Activity B again which has launchMode “SingleTask” then stack will be

a) A
b) B

Activity C and D get removed from second task (b) and Activity B will be root.

4. SingleInstance :

If activity get launched with launchMode “SingleInstance” then activity is always the only member in its task.
Any activity launched with this mode always open in separate task.

Example

You have 4 Activities Activity A, Activity B, Activity C and Activity D. Activity B has launchMode as “SingleInstance”.

If you launch activity A then Activity B after that you launch Activity C and at the end you launch Activity D but Activity D with launchMode “SingleInstance” then stack will be

a) A -> B -> C
b) D

Two task get created (a) and (b) and Activity D get launched with launchMode “SingleInstance” thats why D get created in separate task.

You can also use taskAffinity in this case also.

--

--

Himmat Sawant

I am Software Engineer, I like to code, write and share knowledge