This blog will help you to understand the launch mode of android activity. In this tutorial, we will know the type of launch mode and where to use them according to our need in the application. First, we will explain about term 'task'.
A task is a collection of activities. When user launches a new application from a home screen app icon launcher, that application main activity launch with a new task. The application's all activities belongs to the same task by default.
There are four type of launch mode:
- Standard
- Single Top
- Single Task
- Single Instance
1- Standard :- Its a default launch mode of activity. All the activities which start with this launch mode belong to the same Task.
<activity android:launchMode="standard"/>
Example: Suppose you launch a new Activity A from device home screen. This launched activity will create a new task 'Task A' and add to this task with a new instance. If again you start same activity Activity A from anywhere in an application then Activity A will create a new instance on top of existing instance in Task A.
2- Single Top :- If we launch an activity with attributes Single Top and this launch activity's instance already exists on top of the back stack then does not create a new instance. But if the activity at the top of the back stack is not an existing instance of the activity the activity can be instantiated multiple times.
<activity android:launchMode="singleTop"/>
Example :- Application Back task list contains three activities A-->B-->C if we again launch C activity then doesn't create a new instance of Activity C because it's already exists on top of the back stack. If create another scenario with new Back Stack list contains three activities A-->C-->B and again launch Activity C then a new instance create on back stack and the new back stack list will be A-->C-->B-->C.
3- Single Task :- If we launch an activity with this attributes then activity create new Task and activity will be the root of a task. Others activity which will be launch from this activity will part of this task. Only one instance of activity exists at one time.
Example: - Application back stack contains list task Task A (B-->D) and if launch a new activity C with attributes Single Task then activity C will create new task Task B. Now application back stack task will be
Task A(Activity B-->Activity D)-->Task B(Activity C)
If you launch new activity Activity E from Task B then back stack will be below.
Task A(Activity B-->Activity D)-->Task B(Activity C--> Activity E)
4- Single Instance :- This lunch mode act same as Single Task but the difference is that it doesn't launch any others activities into this task. Always activity launch with only a single instance.
Example: Application back stack contains list Task A(Activity A) and you launch a new Activity B with Single Instance mode then Activity B will create a new Task B(Activity B) and back stack will be as below:
Task A(Activity A)--> Task B(Activity B)
if you again launch new Activity C with launch mode Standard then back stack will be as below:
Task A(Activity A)--> Task B(Activity B)-->Task A(Activity C)
0 Comment(s)