My Home navigation target is dynamic, because it h...
# compose-wear
y
My Home navigation target is dynamic, because it has a ViewPager What is the right way to navigate to the top of the stack? I ended up using a while loop and popBackStack, because it doesn't see to work if I don't know the route exactly. cc @Steve Bower [G]
Copy code
fun navigateToPlayer(clearBackstack: Boolean = true) {
        val route = NavigationScreens.Player.route + "?page=0"
        if (clearBackstack) {
            while (navController.popBackStack()) {}

            navController.navigate(route)
        } else {
            navController.navigate(route)
        }
    }
s
Have you looked at popUpTo and popUpToInclusive (which can be passed as part of the navigate() call)? See

https://www.youtube.com/watch?v=mLfWvSGG5c8

y
I tried that, it didn't work which I put down the the variable part of the route (the query params...). But I'll try again and maybe strip all params from the popupTo?
It kind of makes sense. If I have route epidode?id=1234 and then try to pop up epidode?id=1235 or epidode I wouldn't expect it to be correct, although it doesn't seem to pop up at all if names mismatch, while docs say all non matching get removed.
I'll make a clean repro and file a bug for it silently not removing them.
i
You use the exact string from your
route
, you don't fill in arguments, etc.
mind blown 1
1
y
Thanks, ended up with effectively
Copy code
val topRoute = NavigationScreens.Episode.route + "?id={id}"

val newRoute = NavigationScreens.Episode.route + "?id=0"

...
    navController.navigate(newRoute) {
        this.popUpTo(topRoute) {
            this.inclusive = true
        }
    }
Seems to work nicely.