简体   繁体   中英

Navigate to composable without adding it to backstack

I'm using a Compose Destinations library and I don't understand what should I do to avoid adding destination to backstack.

Explanation: User navigates Screen1 -> Screen2 -> Screen3, then he navigates back (by pressing button or back gesture) and gets into Screen1, because Screen2 isn't in a back stack. Screen1 <-... <- Screen3

I searched for something like this in documentation:

navigator.navigate(SecondScreenDestination) {
    addToBackStack = false
}

But there are nothing like this

I tried to use popUp functions to achieve my goal, but I didn't find the solution. I really need help with this.

When you call navigate() , you add that new destination to the back stack. This means when you navigate to your SecondScreen , your back stack becomes:

FirstScreen -> SecondScreen

If you want to navigate to ThirdScreen and also remove SecondScreen from the back stack, that's exactly what popUpTo does as per the Navigation Compose docs :

navigator.navigate(ThirdScreenDestination) {
    popUpTo(SecondScreenDestination.route) {
        inclusive = true
    }
}

So you're doing two things as part of this single, atomic operation:

  1. Popping every destination up to (that's the popUpTo part) and including (that's the inclusive part) SecondScreen . This is what removes SecondScreen from the back stack.

  2. Navigating to ThirdScreen , which happens after the popUpTo completes, thus putting ThirdScreen on the top of the back stack.

Thus your final back stack is:

FirstScreen -> ThirdScreen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM