Android Activity Launch Mode

Ankit Sinhal
AndroidPub
Published in
5 min readJan 13, 2017

--

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

A task is a collection of activities that users interact with when performing a certain job. In general an application contains number of activities. Normally when user launch an application a new task will be created and the first activity instance is called root of the task.

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

Activities are arranged with the order in which each activity is opened. This maintained stack called Back Stack. When you start a new activity using startActivity(), it “pushes” a new activity onto your task, and put the previous Activity in the 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

This is the default launch mode of an activity (If not specified). It creates a new instance of an activity in the task from which it was started. Multiple instances of the activity can be created and multiple instances can be added to the same or different tasks. In other words you can create the same activity multiple times in the same task as well as in different tasks.

<activity android:launchMode=”standard” />

Example:

Suppose you have A, B, C and D activities and your activity B has “launch mode = standard”. Now you again launching activity B –

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

In this launch mode if an instance of activity already exists at the top of the current task, a new instance will not be created and Android system will route the intent information through onNewIntent(). If an instance is not present on top of task then new instance will be created.

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:

Suppose you have A, B and C activities and your activity D has “launch mode = singleTop”. Now you launching activity D -

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:

Suppose you have A, B, C and D activities and your activity D has “launch mode = singleTop”. Now you again launching activity D -

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

In this launch mode a new task will always be created and a new instance will be pushed to the task as the root one. If an instance of activity exists on the separate task, a new instance will not be created and Android system routes the intent information through onNewIntent() method. At a time only one instance of activity will exist.

<activity android:launchMode=”singleTask” />

Example:

Case 1:

Suppose you have A, B and C activities and your activity D has “launch mode = singleTask”. Now you launching activity D -

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:

Suppose you have A, B, C and D activities and your activity B has “launch mode = singleTask”. Now you again launching activity B-

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

This is very special launch mode and only used in the applications that has only one activity. It is similar to singleTask except that no other activities will be created in the same task. Any other activity started from here will create in a new task.

<activity android:launchMode=”singleInstance” />

Example:

Case 1:

Suppose you have A, B and C activities and your activity D has “launch mode = singleInstance”. Now you launching activity D -

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:

Suppose you have A, B, C activities in one task and activity D is in another task with “launch mode = singleInstance”. Now you again launching activity D-

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

Android also provides Activity flags by which you can change the default behavior of Activity association with Tasks while starting it via startActivity() method. These flags values can be pass through Intent extra data.

FLAG_ACTIVITY_NEW_TASK

This flag works similar to “launchMode = singleTask”.

FLAG_ACTIVITY_CLEAR_TASK

This flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. The activity becomes the new root of an otherwise empty task, and any old activities are finished.

FLAG_ACTIVITY_SINGLE_TOP

This flag works similar to “launchMode = singleTop”.

FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

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.

--

--

Ankit Sinhal
AndroidPub

Senior Software Engineer at Walmart. Dreamer and Achiever..