Is there a right way to use Jetpack Compose Naviga...
# koin
s
Is there a right way to use Jetpack Compose Navigation components with Koin?
a
is it different from Jetpack Navigation? 🤔
s
a
can be interesting to see a concrete sample and what could bring Koin
interesting tought 👍
s
Common usecase. I'm declaring navController like.
Copy code
val navController = rememberNavController()
And connect it to NavHost declaring with some kind of DSL like:
Copy code
NavHost(
        navController,
        startDestination = SCREEN_ONE
   ) {
        composable(SCREEN_ONE) {
            screenOne(
                myViewModel = myViewModel,
                navController = navController
            )
        }
        composable(SCREEN_TWO) {...}
........ all other composables
}
That's all I need to start app with embedded navigation. I'm calling:
Copy code
navController.navigate(SCREEN_IM_GOING)
inside composables. So I can use Koin's ViewModel inside composables. But I steel need to pass navController through all compose tree to use it. Would be nice if Koin will take care of navController.
p
With koin you can inject a navigator instance to composable function containing
NavHost
and pass navController to it, so you can use it in the
ViewModel
.
s
It's not so easy because NavController created not just with plain constructor but "rememberNavController". BTW Hilt already have navigation support features: https://developer.android.com/jetpack/compose/libraries#hilt-navigation
323 Views