In Android, how do the standard, singleTop, singleTask and singleInstance launch modes differ, and when does an activity get onNewIntent instead of a new instance?
answer
- stack is never rearranged
- top of the current task only
- root of a task, clears above
- alone in its task
- the intent flag beats the manifest
basics
~20 sstandard always makes a new instance; singleTop reuses one only on top of the current task; singleTask keeps one at a task root and clears activities above it; singleInstance keeps it alone in its task. Reuse arrives in onNewIntent().
solid answer
~40 s`launchMode` on the `<activity>` element decides how a started activity joins a task. **`standard`**, the default, pushes a new instance every time, so a task can hold several. **`singleTop`** reuses the existing instance only when it is already on top of the current task, calling `onNewIntent()`; anywhere lower in the stack it still gets a new instance. **`singleTask`** puts the activity at the root of a new task, or finds the task with its affinity; if an instance exists it receives `onNewIntent()` and every activity above it is destroyed. **`singleInstance`** behaves like `singleTask` but nothing else may join its task. `singleInstancePerTask` allows one instance per task, as the root. A caller's intent flags such as `FLAG_ACTIVITY_SINGLE_TOP` or `FLAG_ACTIVITY_NEW_TASK` win over the manifest, and `FLAG_ACTIVITY_CLEAR_TOP` has no manifest equivalent.
go deeper
Name the default mode and say that singleTop avoids a duplicate only when the same activity is already on top.
Compare the modes with stack examples, explain onNewIntent and what the user cannot go back to, and say that intent flags beat the manifest.
Debug task surprises from taskAffinity and FLAG_ACTIVITY_NEW_TASK, and know why singleTask without a launcher filter strands users.
Treat launch modes as a last resort: keep in-app navigation inside one task and reserve special modes for real entry points users reach from outside.
## Tasks and the back stack A **task** is the set of activities a user works through to do one thing, kept in a **back stack** in the order they were opened. When one activity starts another, the new one is pushed on top and the old one stops; Back pops and destroys the top one and resumes the one beneath. By default the platform never **rearranges** this stack: activities are only pushed and popped. That single rule explains most launch-mode answers. By default an activity started from several places is instantiated several times, even across tasks, each copy with its own UI state. You change the defaults in two places: the `launchMode` attribute (with `taskAffinity` and a few other task attributes) on the `<activity>` element, and flags on the `Intent` passed to `startActivity()`. If both say something, the **caller's intent flag wins** over the started activity's manifest. ## The five launch modes | `launchMode` | New instance? | Where it lives | Reused through | |---|---|---|---| | `standard` (default) | always | the caller's task | never reused | | `singleTop` | unless already on top of the current task | the caller's task | `onNewIntent()` when on top | | `singleTask` | only if none exists | root of a new task, or the task with its affinity | `onNewIntent()`; activities above it are destroyed | | `singleInstance` | only if none exists | a task of its own, alone | `onNewIntent()`; anything it starts opens in another task | | `singleInstancePerTask` | one per task, as the task root | root only; more tasks with `FLAG_ACTIVITY_MULTIPLE_TASK` or `FLAG_ACTIVITY_NEW_DOCUMENT` | `onNewIntent()`; activities above it are destroyed | Worked examples from the platform's own model: 1. Stack `A-B-C-D`, D on top, and an intent for D arrives. With `standard` the stack becomes `A-B-C-D-D`. With `singleTop` D receives `onNewIntent()` and the stack stays `A-B-C-D`. 2. Same stack, an intent for B arrives and B is `singleTop`. B is not on top, so a new B is pushed: `A-B-C-D-B`. 3. Stack `A-B-C` and an intent for A, which is `singleTask` or `singleInstancePerTask`. A receives `onNewIntent()`, B and C are finished, and the task is just `A`. 4. A `singleTask` activity that already lives in a **background** task brings that whole task forward, and its back stack sits on top of the current one, so Back walks through it first. ## onNewIntent and what the user loses When an existing instance handles a new intent, the platform calls `onNewIntent()` on it instead of creating anything. In the photo editor, a `singleTop` editor that is already open receives a second "edit this photo" request there, so the code must switch to the new photo using the intent passed in. The user **cannot** press Back to return to the state the activity had before that intent arrived, because no new entry was pushed. That is the trade a team accepts when it reuses instances. ## Intent flags - `FLAG_ACTIVITY_NEW_TASK` starts the activity in a different task, often a new one; if a task for it already exists, that task comes forward and the activity gets `onNewIntent()`. Some callers, such as the notification manager, always set it because they start activities in an external task. - `FLAG_ACTIVITY_SINGLE_TOP` gives the `singleTop` behaviour for this one start. - `FLAG_ACTIVITY_CLEAR_TOP` destroys everything above an existing instance in the current task and delivers the intent to it. If the target's mode is `standard`, the target itself is also destroyed and recreated, because a `standard` activity always takes a new instance for a new intent. ## Affinity, the source of task surprises Every activity has a **task affinity**, by default the app's package name, meaning all its activities prefer the same task. `taskAffinity` changes it. Affinity matters in two cases: when the starting intent carries `FLAG_ACTIVITY_NEW_TASK`, the system looks for an existing task with the same affinity before making a new one; and with `allowTaskReparenting="true"`, an activity can move to the task it has an affinity for when that task comes to the foreground. Two related rules: - Use `singleTask` and `singleInstance` only on an activity with an `ACTION_MAIN` and `CATEGORY_LAUNCHER` filter. Otherwise the user can leave the new task with Home and have no launcher icon to return to it. - If the user leaves a task for a long time, the system clears it down to the root activity; `alwaysRetainTaskState`, `clearTaskOnLaunch` and `finishOnTaskLaunch` change that. Most apps never need to change the defaults, and the platform advises testing any change against Back, Recents and launches from other apps.
- An activity started with FLAG_ACTIVITY_NEW_TASK unexpectedly opens inside another of your app's tasks. Why?With `FLAG_ACTIVITY_NEW_TASK` the system first looks for an existing task with the same affinity, and every activity of an app shares the package name as its affinity by default. If a task for that affinity is in the background, the activity lands there instead of a fresh task. Give the activity its own `taskAffinity`, or rethink whether a separate task is needed.
- Why should singleTask be reserved for activities with a launcher intent filter?A `singleTask` activity can start a new task. If the user presses Home, that task goes to the background, and without an `ACTION_MAIN` plus `CATEGORY_LAUNCHER` filter there is no launcher icon that leads back to it. The work in that task becomes hard to reach. For an activity users should not return to, `finishOnTaskLaunch` is the documented alternative.
- singleTop reuses an existing instance found anywhere in the back stack.
- singleTask always creates a brand-new task, whatever the affinity says.
- The manifest launchMode overrides any flag on the calling intent.
- After onNewIntent the user can press Back to the previous state.
- Mark every screen singleTask to avoid duplicate screens.
- By default the back stack reorders to bring an existing activity to the top.