Android Activity "launchMode" Explained, Must-know for Android Development

What happens, when we add this launchMode tag in an activity of an Android application.
<activity android:name=”.MainActivity”
android:launchMode=”singleTop”></activity>
Assume A, B, C, D, E, F are the Activities
launchMode="singleTop"
We are adding launchMode="singleTop" in D.
Example one:
Previous State of Activity Stack
D: D is on top of Activity Stack
C
B
A
Now, Start D from any service or other application or from somewhere.
Final State of Activity Stack
D: old instance gets extras data through onNewIntent(Intent intent);
C
B
A
Example Two:
Previous State of Activity Stack
C
B
A
Start D from any service or other application or from somewhere.
Final State of Activity Stack
D: starts as usual.
C
B
A
Example Three:
Previous State of Activity Stack
C
D
B
A
Start D from C
Final State of Activity Stack
D: As the last D was not on the top of the task, a new instance will be created.
C
D
B
A
launchMode="singleTask"
We are adding launchMode="singleTask" in C.
Example one:
Previous State of Activity Stack
D
C
B
A
Now, Start C
Final State of Activity Stack
C: old instance gets extras data through onNewIntent(Intent intent);
B
A
Info: D gets destroyed
Example Two:
Previous State of Activity Stack
B
A
Now, Start C
Final State of Activity Stack
C: starts as usual.
B
A
launchMode="singleInstance"
We are adding launchMode="singleInstance" in E.
Example one:
Previous State of Activity Stack
D
C
B
A
Now, Start E
Final State of Activity Stack
E
— — — — — — — — — — — — — — — — — — — — — — —
D
C
B
A
Info: A, B, C, D will be in one task and E will be in another task.
and if we continue this, and start F from E, then
Final State of Activity Stack
F
D
C
B
A
— — — — — — — — — — — — — — — — — — — — — — —
E
Info: A, B, C, D, F will be in one task and E will be in another task.
Another Example
Previous State of Activity Stack
A
B
— — — — — — — — — — — — — — — — — — — — — — —
E
Now, Start E from A
Final State of Activity Stack
E: old instance gets extras data through onNewIntent(Intent intent);
— — — — — — — — — — — — — — — — — — — — — — —
A
B
launchMode="standard"
We are adding launchMode="standard" in B.
Previous State of Activity Stack
D
C
B
A
Now, Start B
Final State of Activity Stack
B: a new instance of B
D
C
B
A
Now when we set flag programmatically:
FLAG_ACTIVITY_NEW_TASK
launchMode — singleTask | flag — FLAG_ACTIVITY_NEW_TASK: If an Activity does not exist in an already created Task, then it starts the Activity in a new Task with Activity’s new instance at the root of the Task’s back stack, else the Task is brought forward with the Activity’s last state restored and this Activity receives the new intent in
method. Only one instance of the Activity can exist at a time. In this case, the Back button is still able to return the user to the previous Task’s Activity.onNewIntent
FLAG_ACTIVITY_CLEAR_TASK
It works in conjugation with FLAG_ACTIVITY_NEW_TASK
Example One:
We are starting E from D with a flag.
Previous State of Activity Stack
D
C
B
A
Final State of Activity Stack
E
Info: All others will get destroyed
Example Two:
We are starting B from D with a flag
Previous State of Activity Stack
D
C
B
A
Final State of Activity Stack
B: a new instance of B will be created it will start as usual.
Info: All others will get destroyed
FLAG_ACTIVITY_SINGLE_TOP
FLAG_SINGLE_TOP is similar to launchMode="singleTop"
FLAG_ACTIVITY_CLEAR_TOP
We are starting B from D with a flag
Previous State of Activity Stack
D
C
B
A
Final State of Activity Stack
B: old instance gets extras data through onNewIntent(Intent intent);
A
Info: All others will get destroyed
That's it for now.
For a detailed explanation, please check this blog.
Happy Learning :)
Show your love by sharing this blog with your fellow developers.
Also, Let’s become friends on Twitter, Linkedin, Github, Quora, and Facebook.