Introduction
Since the introduce of Android development, here is that the one among the quality things that haven’t changed is launch modes. Launch modes in Android have a vital role which help developers to launch Activities on how it should go with the current or other task. While implementing navigation in Android apps, you’ll face different requirements, like destroying a collection of back stack Activities if the user entered into a specific Activity or always creating a replacement instance of the Activity (or vice versa).By using the proper launch mode, you’ll achieve the specified navigation behavior in an exceedingly single line of code. To do so, you ought to first know the categories of launch modes and the way they work.
Launch mode is set of instruction for Android development which help you to specifies how the activity should be launched. It instructs how any new activity should be related to this task. Before moving further you initially have to understand about vital topics-
- Task
- Back Stack
Tasks
A task may be a collection of activities that users interact with when performing a specific job. generally an application contains number of activities. Normally when a user try to launch an application then a brand new task are created and also the first activity instance is known as root of the task. When user launches an app from home icon, it navigates through different screens so different activities placed on the highest of 1 another. This collection of activities is thought as tasks.
Back Stack
The management of activities with the order in which they opened by the user or developer is called back stack. Generally when you start new activity with the help of start activity(), it pushes a new activity into your stack and put the old recently previous activity in the back stack. Once you press back button then “pops” the top most activity and remove it from the back stack collection and will take to you back to the previous activity.
There are four launch modes for activity. They are:
- standard
- singleTop
- singleTask
- singleInstance
To work with launch mode you can use “launchMode” attribute In the AndroidManifest inside the <activity> element like-
<activity android:launchMode = [“standard” | “singleTop” | “singleTask” | “singleInstance”] ../>
Now android provide four type of launch mode given below.
1. standard
The default launch mode of an activity is standard mode. This mode will create a new instance of an activity within the task from which it had been started. Multiple instances of the same activity can be created and multiple instances may be added to the similar or different tasks. In other words we can say that you’ll be able to create the identical activity multiple times within the same task as well as in different tasks.
<activity android:launchMode=”standard” />
Example:-
Let’s suppose we have A, B, C and D activities and our activity B has “launch mode = standard”. Now we again launching activity B –
then the state of Activity Stack before launch B would be
A -> B -> C -> D
State of Activity Stack after launch B
A -> B -> C -> D -> B
2. singleTop
In this launch mode if activity’ instance already exists at the top of the currently working task, a brand new instance of the same activity won’t be created and Android system can route the intent information through onNewIntent(). If an instance isn’t present on top of task then new instance are created. Using this launch mode you’ll create multiple instance of the identical activity within the same task or in numerous tasks given that the identical instance doesn’t exist already at the highest of stack.
<activity android:launchMode=”singleTop” />
Example:-
Case 1:
Let’s suppose we have activities like A, B and C and D and activity D has “launch mode = singleTop”. Now if we are launching activity D -
then the state of Activity Stack before launch D would be
A -> B -> C
State of Activity Stack after launch D activity would be
A -> B -> C -> D
Case 2:
Let’s suppose we have A, B, C and D activities and our activity D has “launch mode = singleTop”. Now we again launching activity D -
then the state of Activity Stack before launch D would be
A -> B -> C -> D
State of Activity Stack after launch D activity would be
A -> B -> C -> D
3. singleTask
When we are using this launch mode , a new task will always be created and it will push newly created instance to the task as the root one. it don’t need a new instance or a new instance will not be created If an instance of activity already exists at the top of other separate task,, Android operating system will route the intent information through onNewIntent() method. In this launch mode one instance of activity will exist only at a time.
<activity android:launchMode=”singleTask” />
Example:-
Case 1:
Let’s suppose we have A, B and C activities and our activity D has “launch mode = singleTask”. Now we launching activity D -
then the state of Activity Stack before launch D would be
A -> B -> C
State of Activity Stack after launch D activity would be
A -> B -> C -> D
Case 2:
Let’s suppose we have A, B, C and D activities and our activity B has “launch mode = singleTask”. Now we again launching activity B-
then the state of Activity Stack before launch D would be
A -> B -> C -> D
here is the state of Activity Stack after launch B activity would be
A -> B .
4. singleInstance
This is very special kind of launch mode and only utilized in the applications when we have has only 1 activity. it’s just like singleTask except that no other activities are going to be created within the same task. the other activity started from here will create in an exceedingly new task.
<activity android:launchMode=”singleInstance” />
Example:–
Case 1:
Let’s suppose we have A, B and C activities and our activity D has “launch mode = singleInstance”. Now we launching activity D -
Then the state of Activity Stack before launch D would be
A -> B -> C
then the state of Activity Stack after launch D activity would be
Task1 — A -> B -> C
Task2 — D (here D is other task)
Now if we continue this and start E and D then Stack would be look like-
Task1 — A -> B -> C -> E
Task2 — D
Case 2:
Let’s suppose we have A, B, C activities in one task and activity D is in another task with “launch mode = singleInstance”. Now we again launching activity D-
the the state of Activity Stack before launch D would be
Task1 — A -> B -> C
Task2 — D
after this state of Activity Stack after launch B activity would be
Task1 — A -> B -> C
Task2 — D
Intent Flags
Android also provides Activity flags by which help us to change the default behavior of Activity which is associated with Tasks while starting it via startActivity() method. These flags values is suffer Intent extra data.
FLAG_ACTIVITY_NEW_TASK
This flag similar to “launchMode = singleTask”. and do the similar work as single task.
FLAG_ACTIVITY_CLEAR_TASK
With the help of this flag any existing task is be cleared that will be related to the activity to before the activity is started. The activity would be the new root of an otherwise empty task, and unspecified activities are finished.
FLAG_ACTIVITY_SINGLE_TOP
This flag similar to “launchMode = singleTop”.and do the similar work as singleTop.
FLAG_ACTIVITY_CLEAR_TOP
In this if the activity which we have going to launched is already running in the current task, all of the other activities on top of it will be closed instead of launch same activity’s instance ,and this Intent will be delivered to the (now on top) old activity as a new Intent. There are quite a lot on flags. You could find more about it at Intent.
Conclusion
As you’ll see, each launch mode has its own way of launching Activities. If you need multiple instances of the Activity, then make a choice from Standard and SingleTop. If you merely desire a single instance of the Activity at any given time, then make a choice from SingleTask and SingleInstance.