Android Activity Flags

All about Android activity flags.

[https://www.mobomo.com/2011/6/android-understanding-activity-launchmode/]
[https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en]
[https://developer.android.com/guide/topics/manifest/activity-element.html]
[https://developer.android.com/guide/components/tasks-and-back-stack.html]

Android activity flags specify:

  • When to reuse an activity that has already been launched.
  • What task to place the activity in.

In all cases, when an activity is reused, ''onNewIntent'' is called.

Flags can be specified in both the manifest and intent. In all cases, the Intent flag will override the manifest setting.

Launch Modes (and related Intent flags)

standard

The activity will be created (even if other instances are running). It will be placed on top of the current task stack. This is the default launch mode.

singleTop

The activity is reused only if it is on top of the target task stack. Otherwise it is created on the target task stack. Essentially it is the same as standard unless it is already on top.

Can also be specified by using the intent flag Intent. FLAG_ACTIVITY_SINGLE_TOP.

singleTask

The activity will be reused if it currently exists by switching to its task and terminating all activities that are stacked above it. If not found, it will be launched in a new task.

Can also be specified by using the intent flag Intent.FLAG_ACTIVITY_NEW_TASK.

singleInstance

The activity will be reused if it currently exists by switching to its task. No other activities will be placed on the task. New instances will be started in a new task. This new task will allow no more activities to be placed on it.

TaskAffinity

By default the affinity of the activity is the affinity of the application, and by default, that is the package name.

When you call startActivity() to transition from one Activity to another, if you do not set Intent.FLAG_ACTIVITY_NEW_TASK in the Intent flags, the new Activity will be started in the same task, regardless of the value of taskAffinity.

However, if you set Intent.FLAG_ACTIVITY_NEW_TASK in the Intent flags, the new Activity will still be started in the same task if the new Activity has the same taskAffinity as the taskAffinity of the task (this is determined by the taskAffinity of the root Activity in the task). But, if the new Activity has a different taskAffinity, the new Activity will be started in a new task.

FLAG_ACTIVITY_MULTIPLE_TASK causes FLAG_ACTIVITY_NEW_TASK to always start a new task and skip the search.