All About Activity Launch Modes in Android

Hello friends hi. This post is regarding all about activity launch modes. It is one the most confusing topic and night mare for the android developers.


In this post you will learn about the following points which is also the most important for any android interview...


1)- What is launch modes?

2)- Types of launch modes?

3)- How to define launch modes?

4)- How does the onNewIntent() method works for different launch modes?

5)- What is the difference between singleTask and singleInstance?

6)- When would use singleTask launch mode?

7)- What are the common use cases for each launch mode?


So let's get start....

1)- Here we must know what exactly the launch mode is?

The answer is so simple that the launch mode is an Android OS command that determines how an activity should be launched. It specifies that how every new action would be linked to the new task (STACK).

Now the question is what is a task and a back stack?


*** When doing a job, user engage with a task, which is a set of actions

*** The activités are stacked in the order in which they are opened or started the stack is call as the back stack.


2)- Now the question is what are the types of launch modes?

So basically the there are mainly 4 types of launch modes....


a)- standard

b)- singleTop

c)- singleTask

d)- singleInstance


a)- standard: It is a default launch mode of an activity (If not specified). It launches a new 

instances of an activity in the task from which it was launched.

On the other hands, A new instance of an activity is created every time when it is launched.


Example: Suppose we have A, B, C, and D activities and your activity B has standard launch mode. 

Now 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)

We can see that new instance of B is created again.



b)- singleTask: If the launch mode of an activity is declared as the single task that means there 

will be only one instance of that activity will be exist in the entire system and if the activity is 

already exists then it will be brought to foreground and the onNewIntent() method will be called.


Example: Suppose we have A, B, C activities(A →B →C ) and we are launching D that has a 

singleTask launch mode. In that case, the new instance of D will be created so the current state 

will look like this. 

(A →B →C →D)

Now let suppose if we launch B that also has a singleTask launch mode then current state will look 

like this.

(A →B)

Here old instance gets called and intent data route through onNewIntent() callback. 

Also, notice that C and D activities get destroyed here.



c)- singleTop: If an instance of an activity is already present at the top of the stack, then no new 

instance will be created, instead the onNewIntent() method will be called. And if the activity is 

not exist then a new instance will be created and added to the top of the task as root one.


 Example: Suppose we have A, B, C, and D activities. (A →B →C →D)

If we launch C then a new instance of C will be created as it is not on top.


So it will look like:  (A →B →C →D →C)

Now suppose we have  (A →B →C →D →C)  like this

then we if again launch C activity then in this case new instance will not be created. Instead, we will 

receive the callback on onNewIntent() method.



d)- singleInstance: This is highly unique start option that is only used in a program with a single 

activity. It works as the single task except that no activity is created in the same task, Any 

further activity initiated at this point result in the creation of new task.


Example: Suppose you have A, B, and C activities (A →B →C) and your activity D has a

singleInstance launch mode. 

In this case, if we launch D then D will be launch in the diffrent task. New task for D will be 

created.

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


.....So this is all about the types of launch modes.....




2)- Now coming to the next point that how to specify the launch mode for an activity?

So the answer is quite simple that the launch mode can be specified using the 

(android : launchMode = "type of launch mode") attribute with in activity tag in the manifest file. 

Example: (android : launchMode = "standard") 

Below is the pictorial view for specifying the launch mode....









4)- now the question is how does the onNewIntent() method works for different launch modes?

The answer is: The onNewIntent() method is called when an activity with singleTop, singleTask or 

singleInstance launch mode is relaunch with a new intent while an instance is already exist.

This method provide a way to handle the new intent without creating a new instance of the activity.



5)- Now question is what is the difference between singleTask and singleInstance?

*** singleTask: It ensure that there is only one instance of an activity in entire system but other activity 

launched from it can exist in the same task.

*** singleInstance: It ensure that the activity is the only one in its task, and any activity launched from 

it, will be placed in the separate task, isolating it completely. 



6)- Now the next point is that When would use singleTask launch mode?

The answer is: singleTask would be used when an activity has a single instance throughout the 

system, such as main entry point or the central hub (eg: The Home Screen or the Dashboard)



7)- The next point is that what are the common use cases for each launch mode?

The answer is as following....

a)- standard:- Used for most activities where each new launch creates a new instance.

b)- singleTask:- Used for main entry point or the activity that should not have multiple instances 

such as Home Screen.

c)- singleTop:- Used for activities that can handle multiple intents without creating the new 

instances, such as notification or messages.

d)- singleInstance:- Used for activities that should remain isolated, such as special purpose

screen or tools.



Comments

Popular posts from this blog

All about Fragment in Android

Architectural patterns in Software Engineering