Launch Modes in Android

Alisha Bindal
3 min readAug 31, 2022

--

Launch modes define the behaviour of a new or an existing activity in the task, or specifies an instruction on how an activity should be launched into a task.

The activities in Android implement the STACK data structure. (Last in first out)

What is a Task?
A task is basically a collection of activities that the users interact with when trying to do something in your app.

There are four types of Launch Modes :

  • standard
  • singleTop
  • singleTask
  • singleInstance

To use a launch mode it must be defined in the android manifest in <activity/> element as android:launchMode attribute.

standard launchMode

Default launch mode of the activity. If you don’t set any launch mode to your activity, it will use the standard mode by default.
It creates a new instance of activity every time, even if activity instance is already present.

Example :

Activity A , Activity B, Activity C // they are in the stack
Activity B has the standard launch mode

If you launch B again after C then the stack becomes :

Activity A → Activity B → Activity C→ Activity B

Now there are 2 instances of Activity B

singleTop launchMode

This launch mode is almost the same as standard. Many instances of singleTop activity could be created. The difference is, if an instance of activity already exists on the top of the current stack, onNewIntent() will be called instead of creating new instance. If an instance is not present on top of the task then a new instance will be created.

Example 1:

Activity A , Activity B, Activity C // they are in the stack
Activity C has the singleTop launch mode

If you launch Activity C again then the stack becomes :

Activity A → Activity B → Activity C

NOTE : Activity C is not launched again and the system will route the intent information through onNewIntent()

Example 2:

Activity A , Activity B, Activity C // they are in the stack
Activity B has the singleTop launch mode and if you launch Activity B on top of Activity C then the stack becomes :

Activity A → Activity B → Activity C→ Activity B

NOTE : Activity B is launched again because it was not at the top of the stack.

singleTask launchMode

Activity with this launch mode can have only one instance in the system. New task for activity will be created, if it doesn’t exist. Otherwise, task with activity will be moved to the front and onNewIntent() will be called. Basically only one instance in the system (singleton) of the activity will exist.

Example 1:

Activity A , Activity B, Activity C // they are in the stack
Activity B has the singleTask launch mode

If you launch Activity B again then the stack becomes :

Activity A → Activity B

NOTE : Activity C is removed from the stack and the information is routed through onNewIntent()

What is taskAffinity?

By default all the activities have the same taskAffinity and go into the same task . The default taskAffinity is same as the app package name, what if we want to have two different tasks? To achieve this behaviour we have to taskAffinity.
singleTask
means there can be only one instance of the Activity but doesn’t mean that the task has just one Activity.

singleInstance launchMode

This launch mode is similar to singleTask but one difference is that there will be only one activity will present in a stack it wont allow other activity. If another Activity is called from this kind of Activity, a new task would be automatically created to place that new Activity.

Example 1:

Activity A , Activity B, Activity C // they are in the stack

Activity D is in another task with singleInstance launch mode. In this case, if we launch D again then we will receive the callback in the onNewIntent() method of D activity.

Task1:

Activity A → Activity B → Activity C

Task 2:

Activity D
NOTE : Here old instance gets called and intent data route through onNewIntent() callback

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Alisha Bindal
Alisha Bindal

No responses yet

Write a response