Launch Modes In Android

android apps

 To understand the Launch modes in Android you need to understand Task And Back Stack first

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 stack called Back stack.

task and 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:launchMode = [“standard” | “singleTop” | “singleTask” | “singleInstance”] ../>

There are four types of launch Modes in Android:

  1. Standard
  2. Single Top
  3. Single task
  4. Single Instance

 

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 stack already has an instance of that activity, whenever onCreate() method of an activity is called its new instance is created.
  • Works with FILO(First In Last Out)

standard lauch mode

Single Top Launch Mode

  • In this, if the instance of an activity is already present in stack then it does not create another instance of that activity rather than reuse the same i.e. It will route to same Activity (only if that activity is on top of the stack) else it will create new instance.
  • Single top launch mode routes Activity by “New Intent ()”.

single-top launch mode

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 an 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 as 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 which we can declare as “single task” across a stack, but there can be more than one activity in an application which can be declared as single task but with different task affinity
  • Whenever an activity is launched it will always share the Affinity of its root activity

Example:

Activity B is declare as “single task”

<activity android name:”.Activity_b” android:launch mode:”singleTask” android:taskaffinity=”com.some.task.affinity”/>

 

Step 1:  Activity B is launched so open in another stack

singleTaskstep process

Step 2: Activity C is launch, it will share the affinity of root activity so it will open in stack 2

singleTaskstep process

Step 3: Activity D is launch, it will share the affinity of root activity so it will open in stack 2

singleTaskstep process

Step 4: If Activity B is launch again, then Activity C and Activity D will be pop  and Activity B will be reused.

singleTaskstep process

  • On click of recent button both stacks goes in background, so which ever stack is selected, it will become current stack and activity launched will be open in that current stack.

Single Task without Task Affinity:

  • The instance of activity is reused.
  • All Activities above the launched activity will be removed from stack

singleTaskWithout-Affinity

Single Instance Launch Mode

  • There is no need to declare Affinity in manifest file
  • Whenever an activity is declare as “Single Instance”, then that particular activity will automatically open in another stack.
  • New activity don’t share the affinity of root activity
  • <activity android name:”.Activity_b” android:launch mode:”singleInstance” />

For ex

In given fig,

Activity B is declare as single Instance as launch mode so it automatically open in another stack(Stack B) and all activity launched before and after activity 1, opens in same stack(Stack A).

singleInstance

By Saloni Agarwal