Android launch modes example

hiii Everyone in this blog I am sharing about launch mode in android. In Android, Activity has a multiple launch mode in android.  Launch mode is an instruction for Android which specifies how the activity should be launched. The 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 a user launches an app from the 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 topmost activity and remove it from the back stack and taking you back to the previous activity.

There are four launch modes for activity. They are:

  1. standard
  2. singleTop
  3. singleTask
  4. singleInstance

1. Standard

This is the default launch mode of 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:

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

Using this launch mode you can create multiple instances 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 the stack.

<activity android:launchMode=”singleTop” />

Example 1:

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:

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

singleTask 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 the Android system routes the intent information through the onNewIntent() method. At a time only one instance of activity will exist.

<activity android:launchMode=”singleTask” />

Example:

Case 1:

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:

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 B ==>   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 a very special launch mode and only used in the applications that have 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 a new task.

<activity android:launchMode=”singleInstance” />

Example:

Case 1:

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 the 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:

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.

Facebooktwitterredditpinterestlinkedinmail
Recommended
Hiii Everyone, In this tutorial,  I share how to capture…