Launch modes of android tell us how our activity instances will be maintained in activity history stack.
There are 4 types of launch modes
standard
singleTop
singleTask
singleInstance
1. standard
This is the default launch mode of activity in android. We don’t have to explicitly specify this in AndroidManifest.xml. When an activity is of this launch mode type, it will have multiple instances.
For example if Activity A, which is of standard launch mode type, whenever this activity is launched, it will create new instance of Activity A.
2. singleTop
This is the next type of launch mode which can also have multiple instances of the activity similar to standard type but on certain conditions. We have to specify that for the required activity in AndroidManifest.xml like this
android:launchMode="singleTop"
Whenever an activity falls into this type, on launch of this particular activity, new instance of the activity will only be created if it is not on the top of the recent activity history stack. Otherwise existing instance which is present on top will be re-used.
Activity A(singleTop) -> Activity B(standard) -> Activity A(singleTop)
In the above flow, Activity A is launched at first. Then Activity B is launched. Again Activity is launched. But as activity A is not in top and Activity B is in top, new instance of Activity A is created.
In the same flow, if activity A is again launched, it will re-use existing instance since it was already present on top
Activity A(singleTop) -> Activity B(standard) -> Activity A(singleTop)
3. singleTask
This type of launch mode can only have one instance of the activity and should be specified in AndroidManifest.xml like this
android:launchMode=”singleTask”
Whenever an activity falls into this type of launch mode, on launch of the particular activity, it will create new instance only once. And when launched again, it will re-use the existing instance regardless of whether it is in top or other order of history stack. And while re-using the instance, it will clear stack of all activities below it and then get launched.
For example:
Activity A (standard) -> Activity B (singleTop) -> Activity C (singleTask)
Now if activity C is launched, it will clear history of activity A and activity B and Activity C will be the only activity left in history stack.
4. singIeInstance
This type of launch mode can also have only one instance of the particular activity. We have to specify this launch mode in AndroidManifest.xml like this
android:launchMode=”singleInstance”
This type of launch mode is useful to separate the activity from existing stack and create the instance in a separate task. And in that task also, only one instance of the activity can exist.
For example:
Activity A (standard) -> Activity B (singleTop) -> Activity C (singleTask)
Now if Activity D (singleInstance) is launched, the flow will be like below
Task or Stack 1: Activity A (standard) -> Activity B (singleTop) -> Activity C (singleTask)
Task or Stack 2: Activity D (singleInstance)
As you can see, both are in separate history stack.
Note: Whenever an existing instance has to be re-used in any one of the launch modes like singleTop, singleTask and singleInstance, a method called onNewIntent(Intent intent) will be triggered.
Happy coding..
Let’s connect on LinkedIn https://www.linkedin.com/in/dilip-chandar-97570158?
Top comments (0)