Abhishek Dewan
03/08/2021, 2:42 AMNavHost(navController = navController, startDestination = "splash") {
composable("splash") {
SplashScreen(navController)
}
composable("home") {
HomeScreen()
}
}
and you SplashScreen was defined somewhat like this
@Composable
fun SplashScreen(navController: NavHostController) {
val viewModel: SplashViewModel = viewModel()
The injection will not work if your SplashScreenViewModel takes non zero constructor params. In order to fix this you need to inject the viewModel somewhat like this
val viewModel: SplashViewModel = viewModel(
factory = HiltViewModelFactory(
LocalContext.current,
navController.getBackStackEntry("splash")
)
)
and this is possible by using the androidx.hilt:hilt-navigation:1.0.0-alpha03
library.
I spent the last hour trying to debug why despite everything being right, the injection wouldn’t work 😄Ian Lake
03/08/2021, 2:44 AM