https://kotlinlang.org logo
#compose
Title
# compose
r

Robert Menke

03/06/2022, 4:33 AM
I’m using navigation-compose and trying to use
popUpTo
correctly. I have a registration flow where I (for illustrative purposes) navigate to
A
->
B
->
C
->
D
->
SignedIn
. When I navigate to my
SignedIn
route like so
Copy code
navController.navigate("SignedIn") {
    popUpTo("D") { inclusive = true }
}
and then press back the app goes back to
C
. When I continue to press back it goes to
B
and then
A
. It seems like my call to
popUpTo
is ONLY taking
D
off the back stack rather than popping all previous destinations from the back stack. What am I misunderstanding?
t

Tash

03/06/2022, 4:42 AM
seems like it should work... 🤔 what happens when you don't specify
inclusive = true
?
oh sorry, if you want to pop everything upto and including "A" then you might try:
Copy code
navController.navigate("SignedIn") {
    popUpTo("A") { inclusive = true }
}
r

Robert Menke

03/06/2022, 4:49 AM
I want to pop everything up to and including
D
. When I specify inclusive = false, back navigates back to
D
which is what I’d expect.
t

Tash

03/06/2022, 9:54 AM
So do you want to pop A, B, C and D, i.e. the whole graph? If that's the case then aren't you looking for
popUpTo("A") { inclusive = true }
? See this example for using
popUpTo
for bottom nav bar, esp this snippet:
Copy code
// Pop up to the start destination of the graph to
              // avoid building up a large stack of destinations
              // on the back stack as users select items
              popUpTo(navController.graph.findStartDestination().id) {
                saveState = true
              }
The comment there kinda explains
popUpTo
usage
r

Robert Menke

03/06/2022, 5:15 PM
I’m not sure I understand.
Copy code
// Pop everything up to and including the "home" destination off
// the back stack before navigating to the "friendslist" destination
navController.navigate("friendslist") {
    popUpTo("home") { inclusive = true }
}
The example from the same page indicates it will pop everything up to and including
home
t

Tash

03/07/2022, 1:28 AM
I think you have to think of
popUpTo
in terms of a stack (LIFO). If you have already navigated
A
->
B
->
C
->
D
and you want to clear that, you have to think in terms of popping the current stack one by one till you reach the desired node, so first you'd pop
D
, then
C
, then
B
and finally
A
. If you do
popUpTo("D") { inclusive = true }
then it's just going to pop up to (and including)
D
.
r

Robert Menke

03/07/2022, 1:33 AM
Yep - had that realization after I posted… sometimes I’m so dumb I swear 😅
😅 1
Thanks for the help @Tash I appreciate it 🙂
👍🏼 1