What are launch Modes in Android with examples
What is a Task in Android?
A task is a collection of activities that the users interact with when trying to do something in your app.
Confused ? Look at the below snapshot to understand what is a task
What are Launch modes in Android ?
When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the <activity>
element's launchMode
attribute.
There are four launch modes in Android
- standard
- singleTop
- singleTask
- singleInstance
Note:
The activities in Android implement stack data structure (Last in first out)
standard:
This is the default launch mode of activity. If you don’t set any launch mode to your activity, it will use the standard mode by default. It creates a new instance of activity every time even if activity instance is already present.
Lets consider the below example to understand the standard launch mode better
We have four activities launched in the below order
A -> B -> C -> D
Activity C has the standard
launch mode
Lets launch C again after D then the stack looks like below
A -> B -> C -> D ->C
We can see here that there are 2 instances of Activity C
singleTop
If an instance of activity already exists at the top of the current task, a new instance will not be created and the Android system will route the intent information through onNewIntent().
If an instance is not present on top of the task then a new instance will be created.
Case 1:
A -> B -> C -> D
Activity D has launch mode singleTop and if we launch D again then the activity stack looks as below
A -> B -> C -> D
As we can see the D is not launched again and the system will route the intent information through onNewIntent()
Case 2:
A -> B-> C-> D
Activity C has launch mode singleTop and if we launch C on top of D then the activity stack looks as below
A -> B -> C -> D -> C
Here we can see that C is launched again as its not at the top of the task.
SingleTask:
An activity declared with launch mode as singleTask can have only one instance in the system (singleton). At a time only one instance of activity will exist.
If activity instance is not present then the new instance will be created and if the instance is already present in the system then the onNewIntent() method will receive the callback.
Lets try to understand this with different examples
Example 1:
A -> B -> C -> D
Lets say B has launch mode singletask and now if we try to launch B again on top of D the task looks as below
A -> B
We can see that the C and D instances are removed from the stack and the information is routed through onNewIntent()
Example 2:
What is happening in the above snapshot? Why are we seeing 2 tasks of the same app? The answer to the question is taskAffinity
By default all the activities go into the same task and have the same taskAffinity ( the default taskAffinity is same as the app package name), what if we want to have two different tasks like the gmail app ? Then we can achieve this behaviour using taskAffinity.
Lets say we have 4 activitites ( A, B, C, D) , C has singleTask with some other taskAffinity.
<activity
android:name=".ActivityC"
android:launchMode="singleTask"
android:taskAffinity=".someothertask"/>
Task 1:
A -> B
Task 2: Visible to the user
C -> D
Since D is launched from C it will be attached to same task as C ( C will be the root activity from which D is launched)
Now Task 2 is visible to the user and the Task 1 is in the background. Now if we try to launch C again then the task looks like below
Task 1 :
A -> B
Task 2: Visible to the user
C
Now its evident that there can be only one instance of singleTask activity.
Note:
- User can switch between the tasks. Suppose in the above example if the user goes to Task1 after launching C and then launches D the task looks like below
Task 1 : visible to the user
A -> B -> D
Task 2:
C
Now if the user launches C again from Task 1 then Task 2 comes to foreground ( C is not recreated instead onNewIntent() is called).
2. singleTask means there can be only one instance of the Activity but doesn’t mean that the task has just one Activity
singleInstance
It is similar to singleTask except that no other activities will be created in the same task. If another Activity is called from this kind of Activity, a new Task would be automatically created to place that new Activity.
Case 1:
Suppose you have A, B, and C activities(A →B →C) and your activity D has a singleInstance launch mode. In this case, if we launch D then D will be launch in the different task. New task for D will be created.
Task1:
A →B →C
Task2 : (visible currently)
D (here D will be in the different task)
Now if you continue this and start E then the stack will look as below
Task1: ( visible currently)
A →B →C →E
Task2:
D
Case 2:
Suppose you have A, B, C activities in one task (A →B →C)and activity D is in another task with launch mode singleInstance. In this case, if we launch D again then we will receive the callback in the onNewIntent() method of D activity.
Task1:
A →B →C
Task2:
D (Here old instance gets called and intent data route through onNewIntent() callback)
Note:
- In case of singleInstance if we don’t specify taskAffinity then the user cannot observe different tasks( when user clicks on recent screen) but it will create a separate task
- There can be only one Activity in the task which has singleInstance launch mode where as in case of singleTask there can be many activities in the same task.
Problem Statement:
We have the below activities launched in the order
A -> B -> C -> D -> E-> F
When the user clicks back button the following order must be acheived using launch modes only
F -> C -> E -> D -> B -> A
Please comment down your answers below. Reach out to me in case you have any queries.