[Hilt] Multiple viewmodel instances with different keys crash
See original GitHub issueHello
I have recently swapped to Hilt. Before we used dagger and had the following code which would allow us to have multiple instance of the same viewmodel, but with different keys:
fun <T : ViewModel> getUniqueViewModel(modelClass: Class<T>, key: String): T {
val viewModel = ViewModelProvider.get(key, modelClass)
return viewModel
}
Example of use:
val firstViewModel = getUniqueViewModel(TestViewModel.class, "key1")
val secondViewModel = getUniqueViewModel(TestViewModel.class, "key2")
But when I’m using Hilt I get the following error when I try to get my second instance:
java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered
at androidx.savedstate.SavedStateRegistry.registerSavedStateProvider(SavedStateRegistry.java:111)
at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:50)
at androidx.lifecycle.SavedStateHandleController.create(SavedStateHandleController.java:70)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:108)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at com.test.BaseActivity.getUniqueViewModel(BaseActivity.kt:162)
Doesn’t Hilt allow multiple viewModels with different keys?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:30 (6 by maintainers)
Top Results From Across the Web
Can Hilt Android allow different instances of a viewmodel in ...
You are trying to inject ViewmodelA directly into the second activity. This is an error for @HiltViewModel classes, since Android Jetpack ...
Read more >Use Hilt with other Jetpack libraries - Android Developers
All Hilt ViewModels are provided by the ViewModelComponent which follows the same lifecycle as a ViewModel , and as such, can survive ...
Read more >How To Inject ViewModel With Dagger & What Might Go Wrong
1. Providers map in ViewModelProvider.Factory (with or without multibindings). There are several methods. The easiest way is to inject view ...
Read more >Hilt View Models - Dagger
A @ViewModelScoped type will make it so that a single instance of the scoped type is provided across all dependencies injected into the...
Read more >Android: you're probably leaking ViewModel and might not ...
This would be especially bad if instead of a one shot request, the non-singleton Repository keeps receiving some data until you cancel it....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any update about this issue?
Any new about this issue?