question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Hilt] Multiple viewmodel instances with different keys crash

See original GitHub issue

Hello

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:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:30 (6 by maintainers)

github_iconTop GitHub Comments

27reactions
JiangL08commented, Jul 16, 2021

Any update about this issue?

15reactions
JuanchiFragacommented, Dec 27, 2021

Any new about this issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found