Mihai Voicescu
12/08/2021, 5:06 PMNavHostController
to navigate using the ComposeNavigator
API. I need access to it in certain Compose
functions.
Is using the CompositionLocal
API suitable for efficiently pass it around? Seems like they need access to the same reference and I couldn't find a function that already does that...
Something like this
val navController = rememberNavController()
CompositionLocalProvider(LocalNavHostController provides navController) {
NavHost(navController = navController, startDestination = navController.pathToMainScreen) {
composable(navController.pathToMainScreen) { MainScreen() }
composable(navController.pathToWifiScreen) { WiFiScreen() }
}
}
mattinger
12/08/2021, 5:49 PMmattinger
12/08/2021, 5:50 PMIan Lake
12/08/2021, 5:51 PMmattinger
12/08/2021, 5:53 PMfun MyScreen(...., onNavigateToSomeScreen: () -> Unit)
mattinger
12/08/2021, 5:54 PMmattinger
12/08/2021, 5:54 PMmattinger
12/08/2021, 5:59 PMMihai Voicescu
12/08/2021, 6:13 PMCompositionLocal
as to not "having references to NavController leak into each of your screens".
This approach seems to avoid all the pitfalls and have a good development experience.
What do you guys think? @mattinger @Ian Lakemattinger
12/08/2021, 6:16 PMAdam Powell
12/08/2021, 8:35 PMMihai Voicescu
12/09/2021, 9:16 AM