Why's that every time a navigation composable that's on the bottom navigation creates a new view mod...
f
Why's that every time a navigation composable that's on the bottom navigation creates a new view model when the user clicks on the bottom navigation item i'm using
Copy code
implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha01"
and
Copy code
val viewModel: LatestBooksViewModel = hiltNavGraphViewModel(navBackStackEntry)
i don't want to create a new view model each time the user clicks the bottom navigation since it's just wasting API calls
i
Navigation only saves the state of things on the back stack (i.e., can you press the back button to go back to that destination). You'll want to star the multiple back stack issue for updates on saving/restoring the state of individual bottom nav items: https://issuetracker.google.com/issues/80029773
K 1
f
Does that mean that when navigation comes to support multiple stack, it'll do that independently whether it's composable or fragment?
i
I'd suggest reading the full comment where we cover this: https://issuetracker.google.com/issues/80029773#comment94
- The Fragment APIs for supporting multiple back stacks (thus enabling multiple back stacks even if you do not use Navigation)
- The generic Navigation API that will allow any Navigator (be it Fragments, Compose, etc) to support multiple back stacks
- Rework of the existing Navigators to support multiple back stacks out of the box
👍 1