Skip to content

Instantly share code, notes, and snippets.

@appspell
Created May 11, 2021 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appspell/fb0e5499d08f793d4932f739117af8b5 to your computer and use it in GitHub Desktop.
Save appspell/fb0e5499d08f793d4932f739117af8b5 to your computer and use it in GitHub Desktop.
setContent {
val navController = rememberNavController()
NavHost(navController, startDestination = NavigationDestination.Screen1.destination) {
composable(NavigationDestination.Screen1.destination) {
// option #1 create a component inside NavBackStackEntry,
// which can be helpful if we need to provide more than one object from DI here
val component = DaggerScreen1Component.builder().build()
val viewModel: Screen1ViewModel = daggerViewModel {
component.getViewModel()
}
Screen1(viewModel = viewModel)
}
composable(NavigationDestination.Screen2.destination) {
val viewModel: Screen2ViewModel = daggerViewModel {
// option #2 create DI component and instantly get ViewModel instance
DaggerScreen2Component.builder().build().getViewModel()
}
Screen2(viewModel = viewModel)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment