Android Activity Launch Mode

Launch mode is an instruction for Android OS which specifies how the activity should be launched. It instructs how any new activity should be associated with the current task. Before moving further you first need to understand about very important topics-

1. Tasks

2. Back Stack

Tasks

When user launches an app from home icon, it navigates through different screens so different activities placed on the top of one another. This collection of activities is known as tasks.

Back Stack

Once you press back button then “pops” the top most activity and remove it from the back stack and taking you back to the previous activity.

Figure- Back Stack

Above figure explains representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes. For more details you can also refer .

There are four launch modes for activity. They are:

1. standard

2. singleTop

3. singleTask

4. singleInstance

In the AndroidManifest you can use “launchMode” attribute inside the <activity> element to declare the activity’s launch mode like-

<activity android:launchMode = [“standard” | “singleTop” | “singleTask” | “singleInstance”] ../>

Now let’s look at the differences between launch modes.

1. standard

<activity android:launchMode=”standard” />

Example:

State of Activity Stack before launch B

A -> B -> C -> D

State of Activity Stack after launch B

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

2. singleTop

Using this launch mode you can create multiple instance of the same activity in the same task or in different tasks only if the same instance does not already exist at the top of stack.

<activity android:launchMode=”singleTop” />

Example:

Case 1:

State of Activity Stack before launch D

A -> B -> C

State of Activity Stack after launch D activity

A -> B -> C -> D (Here D launch as usual)

Case 2:

State of Activity Stack before launch D

A -> B -> C -> D

State of Activity Stack after launch D activity

A -> B -> C -> D (Here old instance gets called and intent data route through onNewIntent() callback)

3. singleTask

<activity android:launchMode=”singleTask” />

Example:

Case 1:

State of Activity Stack before launch D

A -> B -> C

State of Activity Stack after launch D activity

A -> B -> C -> D (Here D launch as usual)

Case 2:

State of Activity Stack before launch D

A -> B -> C -> D

State of Activity Stack after launch B activity

A -> B (Here old instance gets called and intent data route through onNewIntent() callback)

Also notice that C and D activities get destroyed here.

4. singleInstance

<activity android:launchMode=”singleInstance” />

Example:

Case 1:

State of Activity Stack before launch D

A -> B -> C

State of Activity Stack after launch D activity

Task1 — A -> B -> C

Task2 — D (here D will be in different task)

Now if you continue this and start E and D then Stack will look like-

Task1 — A -> B -> C -> E

Task2 — D

Case 2:

State of Activity Stack before launch D

Task1 — A -> B -> C

Task2 — D

State of Activity Stack after launch B activity

Task1 — A -> B -> C

Task2 — D (Here old instance gets called and intent data route through onNewIntent() callback)

Intent Flags

FLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_CLEAR_TASK

FLAG_ACTIVITY_SINGLE_TOP

FLAG_ACTIVITY_CLEAR_TOP

There are quite a lot on flags. You could find more about it at Intent.

Thanks for reading. To help others please click ❤ to recommend this article if you found it helpful.

Stay tuned for upcoming articles. For any quires or suggestions, feel free to hit me on Twitter Google+ LinkedIn

Check out my blogger page for more interesting topics on Software development.

Senior Software Engineer at Mastercard. Dreamer and Achiever..

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store