Sunday, 9 July 2017

Android Launch Mode

Activity is one of the most brilliant concept on Android from its well-design architecture on memory management which lets Multitasking works perfectly on this most popular mobile operating system. Anyway, Activity is not just to be launched on the screen. The way it is launched is also concerned. There are so many details in this topic. One of those that is really important is launchMode, which is the one that we are going to talk about in this blog.


There are 4 types of launchMode available. Let's see it one by one.
1. Standard
2. SingleTop
3. SingleTask
4. SingleInstance

1. Standard:  This is the default mode.  We are adding launchMode=”standard” in B.

Assume Activity Stack
D
C
B
A

Start Activity B again, Now the activity stack will be
B -new instance of B
D
C
B
A

2. SingleTop:  We are adding launchMode=”singleTop” in D.

Eg:1
Assume Activity Stack
D - is on top of Activity Stack
C
B
A

Start D from any service or other application or from somewhere.
Output:
D -old instance gets extras data through onNewIntent(Intent intent);
C
B
A

Note: if the activity available in the top of stack, it will update the same instance with new intent data or else it will create a new activity instance.

Eg: 2
Assume Activity Stack
D
C
B
A

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

3. SingleTask: We are adding launchMode=”singleTask” in C.

Assume Activity Stack
D
C
B
A

Start C from any activity or service
Output:
C -old instance gets extras data through onNewIntent(Intent intent);
B
A

Note: it checks whether Activity C is available in the Stack, if yes, update the instance with new data and destroy the other activity at the top. Hence here Activity D gets destroyed.  if activity is unavailable in the stack it create a new instance as usual.

4. SingleInstance: We are adding launchMode=”singleInstance” in E.

Eg 1:
Assume Activity Stack
D
C
B
A

Start E
Output:
E
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
D
C
B
A

Note: A , B , C , D will be in one task and E will be in another task.
and if we continue this,  start F from E,then

Output:
F
D
C
B
A
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -
E

Note: A , B , C , D , F will be in one task and E will be in another task.

Eg 2:
Assume Activity Stack
A
B
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -
E

Start E from A
Output:
E -old instance gets extras data through onNewIntent(Intent intent);
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
A
B

Courtesy: Link1 & Link2

No comments: