Android Activity “launchMode” In Action

Activity stack in different launch mode.

There are the following launch mode

  1. SingleTop
  2. SingleTask
  3. SingleInstance
  4. Standard

Explanation :-

What happens , when we add this launchMode tag in an activity of Android application.

<activity android:name=”.LauncherActivity”
 android:launchMode=”singleTop”></activity>

Assume A , B , C , D , E , F are the Activities

1. launchMode=”SingleTop”

Initial Activity stack

A, B,C,D activity in stack

Case 1. if A,B,C,D if present in stack and when start activity D with launch modeSingleTop

new stack will be

A,B,C,D (D have old instance get data through OnNewIntant()

Case 2:

Previous State of Activity Stack

A, B, D,C

Start D from C

Final State of Activity Stack

A,B,D,C,D -As the last D was not on the top of task, new instance will be created.

Conclusion :- if any activity launch with launch mode singleTop then, if that present in stack at Top, older will be call or new one will be added in stack.

In this case a activity can have multiple instance.(Conditionally)

2. launchMode=”SingleTask”

We are adding launchMode=”singleTask” in C.

Case 1:-

Initial stack of activity A , B, C, D

Start C

Final State of Activity Stack

A, B, C -old instance gets extras data through onNewIntent(Intent intent);

(D will get destroyed)

Case 2:-

Initial State of Activity Stack

A, B and start C with launch mode SingleTask

Final Stack

A, B, C . C will be added in stack as usual.

Conclusion:- if activity launch with SingleTask and it present in stack older will be call ,it min, its root to that activity back (called activity) else it will added normally.

In this case, can have only one instance of an activity.

3. launchMode=”singleInstance”

We are adding launchMode=”singleInstance” in E.

Case 1

activity initial state A, B, C, D

start E from D

final activity stack will be

A >B > C > D | E

info — A , B , C , D will be in one task and E will be in another task.

Case 2

and if we continue this , and start F from E,then

Final State of Activity Stack

E | A>B>C>D>F

info — A , B , C , D , F will be in one task and E will be in another task.

Case 3

Previous State of Activity Stack

E - -| B >A

Start E from A

Final State of Activity Stack

B>A | E -old instance gets extras data through onNewIntent(Intent intent);

Conclusion:- in any activity launch with single instance, it will added as different task stack, and without launchmode singleInstance activity in one task stack.

In this case, can have only one instance of an activity.

4. launchMode=”standard”

We are adding launchMode=”standard” in B.

Initial State of Activity Stack

A>B>C>D

Start B

Final State of Activity Stack

A>B>C>D> B -new instance of B

Now when we set flag programmatically:-

FLAG_NEW_TASK

We are starting E from D with flag

Case 1

Initial activity stack

A>B>C>D

A>B>C>D | E

info — A , B , C , D will be in one task and E will be in another task

Case 2

We are starting B from D with flag

Previous State of Activity Stack

A>B>C>D

Final activity stack

A>B>C>D | B

info — A , B , C , D will be in one task and and another B will be in another task

FLAG_CLEAR_TASK

It works in conjugation with FLAG_NEW_TASK

Example One:

We are starting E from D with flag

Case 1

Previous State of Activity Stack

A>B>C>D

Final State of Activity Stack

E

info -All others will get destroyed

Case 2

We are starting B from D with flag

Previous State of Activity Stack

A>B>C>D

B -new instance of B will be created it will start as usual.

info -All others will get destroyed

FLAG_SINGLE_TOP

FLAG_SINGLE_TOP is similar to launchMode=”singleTop”

FLAG_CLEAR_TOP

We are starting B from D with flag

Previous State of Activity Stack

A>B>C>D

Final State of Activity Stack

A>B -old instance gets extras data through onNewIntent(Intent intent);

info -All others (top)will get destroyed

Also, Let’s become friends on Twitter, Linkedin, Github and Facebook.