Launch modes and tasks

Lifecycle and app components Mid-level Kotlin 2.0+ API 31+ Reviewed By Best Latest Apps

Predict the output

Stack is A, B, C. B is declared with android:launchMode="singleTask" in the manifest (same task). C runs this code. What is the stack after?

// in C
startActivity(Intent(this, B::class.java))
Options
Hint 1 of 2

Is there already a B in the task?

Hint 2 of 2

What happens to Activities above it?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A A, B: C is cleared and B gets onNewIntent

Correct. singleTask brings the existing instance to the front and clears what's above it.

Why not the others?
  • B A, B, C, B. That's standard.
  • C A, B, C, with C getting onNewIntent. C isn't the target.
  • D B alone in a new task. That's singleInstance, or a different task affinity.
Activity task with A, B, C. Compare starting B as standard, singleTop and singleTask.
Text version

Start: Task 1: A → B → C (top) (bottom to top)

What each action does
ActionStack afterWhat happens
standard
startActivity(B)
Task 1: A → B → C → B (top)A new B is created on top of C.
singleTop
launchMode=singleTop
Task 1: A → B → C → B (top)B isn't on top, so a new one is still created.
singleTask
launchMode=singleTask
Task 1: A → B (top)C is cleared and the existing B gets onNewIntent.

30-second answer

standard creates a new instance every time. singleTop reuses the instance only if it's already on top, calling onNewIntent. singleTask keeps one instance in its task and clears anything above it. singleInstance puts it alone in its own task. singleInstancePerTask, added in Android 12, allows one instance per task. Flags like FLAG_ACTIVITY_CLEAR_TOP change this per launch.

Practise saying it · 40 s. No recording, just a timer

Explain launch modes.

Say your answer out loud, then check which of these points you covered:

  • standard always creates
  • singleTop reuses only on top
  • singleTask clears above
  • singleInstance is alone
  • Handle onNewIntent

Deep dive

Most apps with Compose have one Activity and do navigation inside it, which avoids most launch-mode puzzles.

singleTop is common for a search or notification target that shouldn't stack copies.

Task affinity decides which task an Activity belongs to; singleTask activities with a different affinity start a new task.

Handle onNewIntent whenever an instance can be reused, or the new extras are ignored.

Common mistakes

  • Forgetting onNewIntent with singleTop and showing stale data.
  • Using singleInstance for ordinary screens.
  • Fixing back-stack bugs with launch modes when navigation inside one Activity would avoid them.

Senior take & follow-ups

Mention onNewIntent and single-Activity apps; that's where real experience shows.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does FLAG_ACTIVITY_CLEAR_TOP with SINGLE_TOP do?

    Show hint

    Clears Activities above the target and delivers the intent to the existing instance.

  2. When does onNewIntent run?

    Show hint

    When an existing instance is reused instead of creating a new one.

Suggest a fix if something here is wrong or out of date.