Skip to content
tezvyn:

singleTask Launch Mode: A -> B -> C Back Stack

Source: developer.android.comMediumHow cards are made

singleTask Launch Mode: A -> B -> C Back Stack

This tests your understanding of Android's singleTask launch mode and task management. A good answer explains that A and B form a stack in one task, while C launches into a new, separate task, becoming its root.

What's really being asked

This question tests your fundamental understanding of Android's task and back stack management, specifically the singleTask launch mode. The interviewer is checking if you know that singleTask activities behave differently from the default standard mode, particularly how they relate to tasks. They want to see if you can trace the state of multiple tasks, not just a single back stack.

The full answer

A strong answer will walk through the process step-by-step. First, explain that Activities A and B are launched with the default standard mode. They are pushed onto the back stack of the same task. After launching B, the stack is [A, B]. Second, describe the singleTask behavior for C. The system looks for an existing task that C has an affinity for. Assuming this is the first launch of C, a new task is created. Third, state that C becomes the root Activity of this new task. The back stack for this new, foreground task is just [C]. Fourth, clarify the state of the original task. The task containing A and B is moved to the background. Its back stack remains [A, B]. Pressing the back button from C would typically navigate the user back to Activity B in the previous task.

The mistakes people make

A major red flag is confusing singleTask with other launch modes or flags. A common mistake is describing standard behavior, saying the stack simply becomes [A, B, C]. Another error is describing FLAG_ACTIVITY_CLEAR_TOP behavior without the new task context, suggesting the stack becomes [A, C] by clearing B. The defining characteristic of singleTask in this initial launch scenario is the creation of a new task. Candidates also often forget to mention that the original task [A, B] is preserved in the background.

What usually comes next

Be prepared for follow-up questions that probe deeper. "What happens if the user navigates A -> B -> C, and then launches C again from a notification?" (The existing instance of C in its own task is brought to the foreground, and its onNewIntent() method is called). "What if C had taskAffinity set to the same affinity as A and B?" (In this case, C would be launched into the existing task and clear all activities above it, resulting in a stack of [A, C]). "How does singleInstance differ from singleTask?" (singleInstance is stricter: the activity is the only activity in its task).

A concrete example

Imagine a music player app. Your main flow is MainActivity (A) -> AlbumListActivity (B). When you tap a song, it launches PlayerActivity (C), which is singleTask. You want the player to be a distinct entity. The A->B->C flow results in the PlayerActivity appearing in its own task. Pressing back from the player returns you to the AlbumListActivity in the previous task. If you later launch the player from a notification, it brings that same player instance forward instead of creating a new one.

Interview question

Given activities A (standard), B (standard), then C (singleTask), what are the back stacks after C is launched?

  • a.One task has stack [A, B] in the background, and a new task has stack [C] in the foreground.Correct
  • b.The back stack is [A, C].
  • c.The back stack is [C], and the previous activities A and B are destroyed.
  • d.The back stack is [A, B, C].
Why?

The singleTask launch mode creates a new task for Activity C, making it the root of that task, which then moves to the foreground. The original task containing Activities A and B is preserved and moved to the background. Option D describes the default 'standard' launch mode behavior, not singleTask.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles