Skip to content
tezvyn:
Menu

Android's Task and Back Stack Explained

Source: developer.android.comMediumHow cards are made

Android's Task and Back Stack Explained

Think of the back stack as a stack of plates (screens). A "Task" is the entire stack for a user's workflow. The system manages this stack, but you can control it for custom navigation. The biggest footgun is misusing launch modes, which breaks back button.

Why it exists

To create a consistent and predictable navigation model for users. Without a structured task and back stack, pressing the back button would be chaotic, and switching between apps would lose the user's place, making the OS feel broken and unusable.

The mental model

An Android Task is a user's workflow, and the back stack is a stack of plates representing the screens (Activities) in that workflow. When you navigate to a new screen, you add a plate to the top. When you press the back button, you remove the top plate, revealing the one underneath. This LIFO (last-in, first-out) structure is what makes navigation feel intuitive.

How it works

When you launch an app, Android creates a new Task and places the main Activity at the bottom of the back stack. As you navigate, new Activities are pushed onto the top of the stack. Pressing the back button pops the current Activity, destroying it and showing the previous one. Developers can override this default behavior using launchMode attributes (standard, singleTop, singleTask, singleInstance) in the AndroidManifest.xml file or by using Intent flags like FLAG_ACTIVITY_NEW_TASK when starting a new Activity.

When to use it

You should manually manage the task and back stack in specific scenarios. For example, when a notification should open a specific screen and clear any intermediate ones. Or when you have a "home" or "dashboard" activity that should never have more than one instance in the stack. Another case is when an activity logically belongs in a separate task, like a document viewer launched from an email client.

When not to use it

For the vast majority of intra-app navigation, you should not interfere with the default behavior. Standard navigation flows where one screen leads to the next are handled perfectly by the default system. Over-manipulating the back stack is a common source of bugs and leads to a confusing, unpredictable user experience.

One canonical example

A user is in a messaging app (Activity A). They tap a web link in a message. The system starts a browser app in a new, separate Task to display the webpage (Activity B). The user reads the page and presses the back button. They are returned directly to the messaging app (Activity A). The browser's task still exists in the background, but the user's primary task was uninterrupted. This separation keeps navigation contexts clean.

Interview question

When a user taps a link in your app, launching an external browser, what is the expected behavior upon pressing the back button from the browser?

  • a.The user navigates to the browser's previous webpage, remaining within the browser's task.
  • b.The user returns to your app, as the browser typically runs in its own distinct task.Correct
  • c.The user exits both the browser and your app, returning to the device's home screen.
  • d.The user is prompted to choose between returning to your app or staying in the browser.
Why?

The card's canonical example explicitly states that launching an external app like a browser typically creates a new, separate Task. When the user presses the back button from this new task, they are returned to the original app's task. Option A is incorrect because while the browser has its own back stack, the primary action when returning from a separate task is to go back to the originating task.

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

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

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