About *Navigation*: what does it actually happen w...
# compose
d
About Navigation: what does it actually happen when you call
NavHostController.navigate()
as in:
Copy code
val navController = rememberNavController()
navController.navigate("detail")
It seems no recomposition is triggered. What is the basis on which Compose is able to update the screen without a recomposition? And how can we achieve the same in a Compose non-Android app?
i
Well, for one, you should never be calling imperative code, like navigate, as part of composition. I assume your code is just for demonstration purposes and you aren't actually doing that
That being said, the source of truth is the NavController's
currentBackStackEntryAsState()
- that state is updated when you navigate and that is what causes your UI to recompose (only on the parts of the hierarchy that depend on said state)
That includes the
NavHost
itself, which certainly recomposes when you change destinations
d
you should never be calling imperative code, like navigate, as part of composition
yes, of course, that code was a simplification. I am always calling
navigate
from onClick functions
Ok, I understand it now. So it’s just recomposing anything that is inside NavHost. Thanks
i
Or any bottom nav, etc. that uses
currentBackStackEntryAsState()
- see the example in the docs: https://developer.android.com/jetpack/compose/navigation#bottom-nav
l
About the BottomNav, is there a way to avoid the view to recompose when switching between tabs ? Let's say I have a BottomBar with Tab A being a list, and a Tab B being a mapView. In Tab A I scroll the list, then open Tab B and I move the map. Then switch to Tab A, I would like my list to have the position as it where before. Same if I go back to the mapView, I don't want to lose the map moved by the user.
i
When it comes to saving state for each bottom nav item, I have good news: https://twitter.com/ianhlake/status/1391908231845322752
l
That's amazing 🎉
Thanks for your hardwork for bringing this alive!