I have a ViewModel that I'm already injecting into a Composable. Now I want to inject the same instance of that ViewModel into my Activity. For example:
In AccountScreen.kt
@Composable
fun AccountScreen(accountViewModel: AccountViewModel = hiltViewModel()) {
...
}
and my Activity class:
class MainActivity : ComponentActivity() {
@Inject
lateinit var accountViewModel: AccountViewModel
}
should have the same instance of AccountViewModel.
I know using @Inject
in the Activity as in the example above doesn't work. Hilt's documentation suggests using ViewModelProvider
or by viewModels()
instead, both of which give me a new instance of AccountViewModel, but I need the same instance as what's in the AccountScreen Composable.