Skip to content
tezvyn:

Describe the back stack for A -> B -> C with singleTask

Source: developer.android.comMediumHow cards are made

Describe the back stack for A -> B -> C with singleTask

Tests understanding of the singleTask launch mode. A great answer explains that launching C destroys B, as singleTask clears all activities above it in the task. The final back stack becomes [A, C]. A red flag is confusing this with singleTop.

What's really being asked

This question evaluates your precise knowledge of Android's launchMode attribute, specifically the behavior of singleTask. Interviewers want to see if you understand how different launch modes manipulate the task's back stack, which is crucial for building predictable and user-friendly navigation. It separates candidates who have a surface-level understanding from those who grasp the underlying mechanics of the Android framework.

The full answer

A strong answer walks through the process step-by-step. First, state the initial stack after navigating A -> B is [A, B]. Second, explain that when Activity C is launched with launchMode="singleTask", the system checks the current task. Assuming default task affinity, it creates a new instance of C. Third, and most importantly, it invokes the "clear top" behavior. This means any activities in the task that are above the new instance are destroyed. In this scenario, Activity B is destroyed. Fourth, state the final back stack is [A, C].

The mistakes people make

A frequent red flag is confusing singleTask with other launch modes. A candidate might say the stack becomes [A, B, C], which is the behavior for standard or singleTop in this scenario. Another common error is describing singleInstance behavior, suggesting C is launched in a completely new, separate task. This only happens with singleInstance or if C's taskAffinity is explicitly set to be different. A vague answer that just says "C is brought to the front" without explaining what happens to B is also a weak signal.

What usually comes next

Be prepared for follow-up questions like: "What happens if the user navigates A -> B -> C -> B and then to C again?" (The second launch of C would call onNewIntent() on the existing C instance and destroy B, leaving the stack as [A, C]). Another is: "How would the behavior change if C was singleInstance?" (C would be created in its own new task, leaving the original task's stack as [A, B]. Pressing back from C would return to B). "What if C had a different taskAffinity?" (It would create a new task with C at the root).

A concrete example

In the initial state, the task's back stack is [A, B]. When the intent to launch Activity C is fired, the Android system processes its singleTask launch mode. It creates a new instance of C and places it on the stack. It then clears all activities above it. Since B was on top of A, B is destroyed. The final back stack contains only [A, C]. If the user presses the back button from Activity C, they will navigate back to Activity A, as B is no longer in the history.

Interview question

An app navigates from Activity A to B. It then launches Activity C, which has `launchMode="singleTask"`. What is the final state of the task's back stack?

  • a.[C]
  • b.[A, B, C]
  • c.[A, C]Correct
  • d.[A, B] in one task, and [C] in a new task
Why?

Launching an activity with `singleTask` clears all activities on top of it in the current task. Thus, Activity B is destroyed, leaving the stack as [A, C]. The stack would be [A, B, C] only if C had the default `standard` launch mode.

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