To understand the Launch modes in Android you need to understand Task And Back Stack first
Table of Contents
Task And Back Stack
TASK: — A Task is a stack of activity i.e. a Task is a group of activities arranged in a Stack.
Back Stack: — Order of Activities in which they are opened reside in a stack called Back stack.
Launch Modes:-
- Launch Modes in Android define how a new or the existing instance of an activity is associated with the current task
- Launch modes in Android can also be set through Flag in Intent
- Or Declare in Manifest file
<activity android:launch mode = [“standard” | “singleton” | “single-task” | “single instance”] ../>
There are four types of launch Modes in Android:
- Standard
- Single Top
- Single task
- Single Instance
Also Read : List Of Unique Android App Ideas for Your Next Business
Standard Launch Mode:
- By default, android launches activities in standard mode.
- It always creates a new instance of an activity. Without taking into consideration that the stack already has an instance of that activity, whenever the onCreate() method of an activity is called its new instance is created.
- Works with FILO(First In Last Out)
Single Top Launch Mode
- In this, if the instance of an activity is already present in the stack then it does not create another instance of that activity rather than reuse the same i.e. It will route to the same Activity (only if that activity is on top of the stack) else it will create a new instance.
- Single top launch mode routes Activity by “New Intent ()”.
Single Task Launch Mode
Single Task can be with Affinity and Without Affinity
To understand Single task Launch Mode in Android you need to understand “task affinity” first.
TASK AFFINITY:
- Task affinity means a single application can have more than one stack
- You can give any name to stack (android:taskaffinity=” com.some.task.affinity).
- E.g, There are two stack
Stack A which is named like the package name
Stack B which is named as com.some.task.affinity 2 task
<activity android name:”.Activity_b” android:launch mode:”singleTask” android:taskaffinity=”com.some.task.affinity”/>
Single Task With Task Affinity:
- There can be only one activity that we can declare as “single-task” across a stack, but there can be more than one activity in an application that can be declared as a single task but with a different task affinity
- Whenever an activity is launched it will always share the Affinity of its root activity
Example:
Activity B is declared as “single-task”
<activity android name:”.Activity_b” android:launch mode:”single-task” android:taskaffinity=”com.some.task.affinity”/>
Step 1: Activity B is launched so open in another stack
Step 2: Activity C is launch, it will share the affinity of root activity so it will open in stack 2
Step 3: Activity D is launch, it will share the affinity of root activity so it will open in stack 2
Step 4: If Activity B is launch again, then Activity C and Activity D will be pop, and Activity B will be reused.
- With on click of a recent button, both stacks go in the background, so whichever stack is selected, will become the current stack, and the activity launched will be open in that current stack.
Single Task without Task Affinity:
- The instance of an activity is reused.
- All Activities above the launched activity will be removed from the stack
Single Instance Launch Mode
- There is no need to declare Affinity in the manifest file
- Whenever an activity is declared as “Single Instance”, then that particular activity will automatically open in another stack.
- New activities don’t share the affinity of root activity
- <activity android name:”.Activity_b” android:launch mode:”single instance” />
Forex
In the given fig,
Activity B is declared as a single Instance as launch mode so it automatically opens in another stack(Stack B) and all activity launched before and after activity 1, opens in the same stack(Stack A).
By Saloni Agarwal