# Jetpack Compose clear back stack, popBackStack inclusive explained

DevFeed: [Jetpack Compose clear back stack, popBackStack inclusive explained](<https://devfeed.tech/articles/jetpack-compose-clear-back-stack-popbackstack-inclusive-explained-25849.md>)

Original publisher: [Read original article](<https://medium.com/@banmarkovic/jetpack-compose-clear-back-stack-popbackstack-inclusive-explained-14ee73a29df5?source=rss-8aced922ece------2>)

Author: Ban Markovic

Published: 2023-01-03T10:31:50Z

Content type: tutorial

Language: en

Sources: [Stories by Ban Markovic on Medium](<https://devfeed.tech/sources/stories-by-ban-markovic-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [navigation](<https://devfeed.tech/topics/navigation.md>), [Android](<https://devfeed.tech/topics/android.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [compose](<https://devfeed.tech/tags/compose.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [navigation](<https://devfeed.tech/tags/navigation.md>), [route](<https://devfeed.tech/tags/route.md>), [screen](<https://devfeed.tech/tags/screen.md>), [state](<https://devfeed.tech/tags/state.md>)

## AI overview

A tutorial on managing navigation back stacks in Android apps built with Jetpack Compose. It explains opening screens with NavHostController.navigate, closing the current screen with popBackStack, and closing multiple screens using a destination route and the inclusive boolean parameter.

## Source excerpt

Jetpack Compose clear back stack, popUpTo/popBackStack inclusive explained Building the navigation for our Android app that is built with Jetpack Compose requires usage of navigation-compose library that is provided by Android team. In order to master closing the screens and tracking our back stack state, it is important to understand the stack data structure that is used for this purpose. Before we can close multiple screens, we need to open them. That's why I decided to separate this article into three sections that represent the most common ways used when navigating through apps: Opening a new screen, Closing current screen, Closing multiple screens at once. Open new screen Navigating from one to another screen is pretty straightforward using navigation-compose lib, we can just use NavHostController and call navigate(...) function with the route as parameter. This will lead into creating a new screen, that is going to be placed on top of our current screen. Example Let's say we have our Login screen opened, and after successful email and password input, we want to load Home screen, our stack would look like this. Close current screen Apart from navigating to a new screen, closing the currently opened one is also a pretty common action in navigation. To achieve this, we can use popBackStack() function from our NavHostController. As the name of the function suggests, it will take the top screen from the stack, and it will remove it. Example Removing our Home screen by calling the popBackStack function will make our stack look like this. Close multiple screens The past two examples were pretty simple, but I wanted to make sure that we understand how navigation-compose lib is storing the data regarding the order of our opened screens. When it comes to closing multiple screens, we can actually use an already known function, popBackStack, but we will need to pass two parameters: route -- represents the destination (screen) that already exists in our stack, and it will ena