Activity launch modes: Android

Task Affinity

The affinity indicates in which task, activity refers to belongs to. By default, all activity belongs to the same task. But it can be modified using taskAffinity an attribute of the <activity> element. This attribute takes a string value (unique from the package name declared in the manifest element). The reason for this unique value is that the android system uses that name to identify the default task affinity for the app ( package name defines the default task affinity).

<activity android:name=".ActivityD"
android:launchMode="singleTask"
android:taskAffinity="com.affinity.of.a"/>
<activity android:name=".ActivityC"
android:taskAffinity="com.affinity.of.c"
android:launchMode="singleTask"/>
<activity android:name=".ActivityB"
android:taskAffinity="com.affinity.of.b"
android:launchMode="singleTask"/>
<activity android:name=".ActivityA"
android:taskAffinity="com.affinity.of.a"
android:launchMode="singleTask"/>
  1. Standard
  2. Single Top
  3. Single Task
  4. Single Instance

Standard launch mode

This is the default one; every time you launch an activity with this launch mode, it will create a new instance of activity and will be moved to the stack.

Single Top launch mode

If we launch an activity with a single top launch mode, it will check whether it has the same activity at the top of the stack. If the same activity is at the top, no new instance will be created; if it's not at the top, a new instance will be created.

Single Task launch mode

In this case, a new task will be created, and a new instance of activity will be moved at the top of the new stack (if this new activity have different taskAffinity). We have two scenarios here :

  1. What if we don't have any instance of this activity in a stack?

Single Instance launch mode

Single Instance works similarly as Single task the only difference is it doesn’t launch any other activities into the task holding the instance. Basically, it will contain a single instance of the activity in a task; any activities started by this one open in a separate task.

  1. What if we don’t have any instance of this activity in the different stack?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store