How to inject a ViewModel into a composable function using Hilt (Jetpack Compose)

From version androidx.hilt:hilt-navigation-compose:1.0.0-alpha02 you can inject view model into Composable functions by: hiltViewModel<ViewModelType>() Example: @Composable fun LoginScreen(viewModel: LoginViewModel) {} LoginScreen( viewModel = hiltViewModel<LoginViewModel>() ) Android Developer Documentation compose and hilt UPDATE: import androidx.hilt.navigation.compose.hiltViewModel @Composable fun LoginScreen( viewModel: LoginViewModel = hiltViewModel() ){ val videos=vm.watchLater.observeAsState() val context= LocalContext.current }

Injecting a repository into a Service in Android using Hilt

From the documentation on how we Inject dependencies into Android classes, we can learn the following: Hilt can provide dependencies to other Android classes that have the @AndroidEntryPoint annotation. Hilt currently supports the following Android classes: Application (by using @HiltAndroidApp) ViewModel (by using @HiltViewModel) Activity Fragment View Service BroadcastReceiver So when you subclass any of … Read more