I'm facing a weird issue with navcontroller when u...
# compose
o
I'm facing a weird issue with navcontroller when using it in the following way to navigate between NavigationBarItems
Books
-
Camera
-
Profile
Copy code
fun handleBottomNavigation(
    navController: NavHostController,
    route: String,
) {

    Log.d("GRAPH", "handleBottomNavigation current destination: ${navController.currentDestination}")

    navController.navigate(route) {
        popUpTo(navController.graph.findStartDestination().id) {
            saveState = true
        }
        launchSingleTop = true
        restoreState = true
    }
}
this leads to: from `BooksScreen`:
navigate(BookDetail)
screen -> from BookDetail
navigate(BuyBook)
which happens to be a different NavigationBarItem->
Camera
NavigationBarItem is now selected -> press back to
Books
NavigationBarItem -> currentDestination in log stays
Camera
, does not navigate,
$route
is
Books
,
navController.graph.findStartDestination().route
is also
Books
just does not navigate removing
restoreState = true
or
save_state = true
from handleBottomNavigation solves it of course but I want to keep state so that I can see
BookDetail
screen again when I press on the
Books
NavigationBarItem, I don't want to get thrown to back to
BooksScreen
navigation.mp4
i
It looks like the poblem is your 'Take Photo' button doesn't actually switch to the Photo tab's back stack - it just adds the destination to whatever the currently selected tab's back stack (your book tab). You'll want to use the same restore+save code in your Take Photo button if you want to swap to the Photo tab's back stack
o
aha, yes I got confused by the handleBottomNavigation function, there's something about the way it looks that made me think this is a Builder for all nav actions
this saveState and restoreState is everytime to be done
i
It is doing exactly what you said to do - pop the current stack up to the start destination of your graph (saving that stack for later restoration), then restore the back stack associated with where you are navigating to
o
and that's "switching to the photo tab's back stack"
i
yep, saving the current stack and restoring the camera tab's stack
o
and that was missing when i was going from book detail to take photo, and got orphaned
or deserted
i
no, it is just on the book tab's back stack
So when you go to select the book tab, look at what it is doing: popping everything on the stack up to the start destination, saving it to the current stack - the book tab stack. Then you go to restore the book tab stack - which is exactly what you just saved
So it looks like you didn't do anything, but you actually saved the whole book stack, then immediately restored it back
o
yea cause nothing changed, from its view
so helpful Ian, thanks
i
if you want to use the current destination and its navigation graph as the source of truth for what tab is supported (which is what the guide has you use), then you need to make sure that when you swap navigation graphs, you correctly save/restore them to keep each back stack separate
o
this is a mental overhead that I have to keep when dealing with tabs then, which the guide doesn't mention
yea the guide mentions nothing of this actually
they rely on people who understand truly how the navigation is working with stacks like you said, haha and that ain't me
i
You might also consider reading the specific guide for Multiple Back Stacks: https://developer.android.com/guide/navigation/backstack/multi-back-stacks That also links out to even more resources
There's ongoing work to make the Navigation Components docs (which apply to both Navigation with Fragments and Navigation Compose) have good examples of using each API because the concepts are actually identical between the two systems
since it is actually only just a single system - the Navigation Component 🙂
o
oh the same one I use for Fragments, hah ok yea since then i never learned it well 😄
that multiple backstacks is it, that's the doc, nice thanks a lot