Describe the back stack for A to B to C with singleTask

Tests singleTask plus taskAffinity, not rote memorization. With default affinity, C sits atop B in the same task; if C already existed, the system clears B and delivers onNewIntent. Red flag: claiming singleTask always spawns a new task.
WHAT THIS TESTS: Whether you understand that singleTask behavior is governed by taskAffinity, not just the launchMode attribute alone. Many developers memorize that singleTask creates a new task, but the real behavior depends on whether the activity shares the current task affinity. The interviewer wants to see if you can explain stack manipulation, instance reuse via onNewIntent, and the difference between first launch and re-entry.
A GOOD ANSWER COVERS: Four points in order. First, because A, B, and C share the default taskAffinity in the same application, launching C from B places C on top of the current task; the back stack becomes A, B, C. Second, if C already existed somewhere in the back stack, the system would clear every activity above it, including B, and route the intent to the existing instance through onNewIntent; the stack would collapse to A, C. Third, if C had been assigned a different taskAffinity, the system would either create a new task with C at the root or bring an existing matching task to the foreground. Fourth, only one instance of a singleTask activity can exist system-wide within its affinity scope, so the activity is never duplicated.
COMMON WRONG ANSWERS: Claiming that singleTask unconditionally creates a new task regardless of affinity. Saying that the stack always clears above C even on the first launch. Describing singleTask as identical to singleTop. Forgetting to mention onNewIntent when an existing instance is reused. Asserting that pressing back from C would always return to B without acknowledging the new-task edge case.
LIKELY FOLLOW-UPS: What happens if the user presses back from C when it is in a new task? How does singleTask differ from singleTop? What if the intent contains FLAG_ACTIVITY_CLEAR_TOP or FLAG_ACTIVITY_NEW_TASK? How would you debug an unexpected task stack in the Recents screen? What happens to activity lifecycle callbacks for B when C clears it from the stack?
ONE CONCRETE EXAMPLE: Imagine a music player Activity declared as singleTask. From a playlist screen B, the user taps a song that launches player C. With default affinity, C sits on top of B in the same task. Later, a notification tap tries to reopen the player. Because the singleTask instance already lives in the task, the system delivers the new track URI via onNewIntent and pops any screens above C, ensuring the player remains the sole instance and the user returns to it without duplicate activities.
Source: developer.android.com
Read the original → developer.android.com
- #android
- #activity-lifecycle
- #launchmode
- #back-stack
- #task-affinity
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.