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.
Read the original → developer.android.com
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.