LaunchModes in Android
Tags: android, kotlin
Published: 2020-07-11 13:37:59

LaunchModes is specific to the android concept. It's one of the interview questions. We will see below what are all the launch modes have in android.

There are 4 types of launch modes
1. Standard
2. SingleTop
3. SingleTask
4. SingleInstance

If we wanted to use any of these, we have to explicitly declare in the AndroidManifest file by using the keyword as "android:launchMode"

 Ex:         android:launchMode="singleTask"

Instead of declaring in the manifest file, we can even set in Java/Kotlin code by using Intent flags. 

FLAG_ACTIVITY_SINGLE_TOP -> Which is similar to singleTop
FLAG_ACTIVITY_NEW_TASK -> Which is similar to singleTask
FLAG_ACTIVITY_CLEAR_TOP 

Will see more about each launch mode's below individually.

-----------------**************************---------------
#Standard:- 
     This is a default launch mode in android. If you won't specify anything in the manifest file then the launch mode type is "standard". Android internally maintains stack. Whatever we open activity by default it will be added to the stack.
     
For example, we have four different types of activities like A, B, C, and D.

Now from Activity A -> launch Activity B -> launched Activity C -> launched Activity D. (A -> B -> C -> D)

All these activities will open in a single stack. Now from Activity_D try to open Activity_B, then a new instance of Activity_B will be created and it will be added to the same back stack. Now will be like
    
             (A -> B -> C -> D -> B)

#SingleTop:-

   If we want to use this launch mode then in manifest.xml for the respective activity we have to set like  

       android:launchMode="singleTop"

For example, we have four different types of activities like Activity_A, Activity_B, Activity_C, and Activity_D.

If we launch activities like A->B->C->D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch C it will create one more new instance and it will be added to back stack.

       A -> B -> C -> D -> C -> C

We declared Activity_C as "singleTop" in manifest like below.
 
 <activity
      android:name= Activity_C
      android:launchMode="singleTop"/>

Now, if we launch activities like A->B->C->D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch "Activity_C" it will not because already "Activity_C" is on top. In this case "*onNewIntent()*" method will be called.
     
        A -> B -> C -> D -> C  (==> here again try to launch C it won't because "C" is a singleTop)
       
below case's are possible
          A -> B -> C -> D -> C -> B -> C
          A -> B -> D -> C -> B -> B -> C -> A -> C  

#SingleTask:-

SingleTask is most frequently used by developers. The main advantage of singleTask is only one instance of activity will be present in a stack.

If we launch activities like A->B->C->D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch C it will create one more new instance and it will be added to back stack.

   A -> B -> C -> D -> C -> C

Now, "Activity_C" defined as *singleTask* we have declared like 

 <activity
      android:name= Activity_C
      android:launchMode="singleTask"/>

then, if we launch activities like A->B->C->D from D if we try to launch "Activity_C" then what are all activities opened on top of Activity_C will be cleared. Now stack will have only

           A -> B -> C 

here "Activity_C" is a singleTask so if we try to launch "Activity_C" again "*onNewIntent()*" method will be called it won't create a new instance and what are all activities opened on top of "Activity_C" will be poped off.

#SingleInstance:-

The name itself says stack will be only one instance. Will see the example below.
 
If we launch activities like A->B->C->D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch C it will create one more new instance and it will be added to back stack.

   A -> B -> C -> D -> C -> C

Now, "Activity_C" defined as *singleInstance* we have declared like 

 <activity
      android:name= Activity_C
      android:launchMode="singleInstance"/>

then, if we launch activities like A->B and from "B" try to launch "Activity_C" then it will be opened in new stack, it will not open in the same stack.

like     A -> B  is in one stack.
only     C will be in another stack.

As of now, we launched A, B, and C activities, now if we try to launch "Activity_D" then which this activity will be added to which stack?

The answer is it will be added to the first stack only means 

           A -> B -> D  is in one stack.
           C will be in another stack.
 
why Activity_D pushed into the first stack? why not newly created stack. Here we have to understand the concept called "taskAffinefty"