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))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.
Text version
Start: Task 1: A → B → C (top) (bottom to top)
| Action | Stack after | What happens |
|---|---|---|
standardstart | Task 1: A → B → C → B (top) | A new B is created on top of C. |
singleToplaunch | Task 1: A → B → C → B (top) | B isn't on top, so a new one is still created. |
singleTasklaunch | Task 1: A → B (top) | C is cleared and the existing B gets onNewIntent. |