You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The idea was that each PageScreen would have it's own viewModel, as they should load data based on which page you're on. However when running this, the following exception occurs:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.app, PID: 8364
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:109)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:76)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:63)
< stacktrace leading to PageScreen, to a line that's past the end of file >
Without passing the key to viewModel() I seem to get same instance for all. I've read that this has been fixed, but the solution was using NavHost, that I'm not using.
We have not fixed the root issue which happens with same class ViewModel with different keys in the sameViewModelStoreOwner, we are still working on the changes needed in lifecycyle-viewmodel to be able to properly have a fix in Hilt.
With a NavHost the issue is avoided since with Navigation Compose each destination is backed by a NavBackStackEntry which itself is a ViewModelStoreOwner. In other words, because each Navigation 'screen' has its own ViewModel scope you totally avoid the issue because the ViewModel class will be unique for that store / scope. I would encourage you to take a look at using Navigation since it also has the benefit of scoping the ViewModel to where it is needed, where as right now all your ViewModels would be scoped to the Activity and in fully Compose app, it can be a lot of ViewModels retained for screens that might not be showing anymore. See: https://developer.android.com/jetpack/compose/navigation
I have an activity like this:
where PagesScreen looks like:
The idea was that each PageScreen would have it's own viewModel, as they should load data based on which page you're on. However when running this, the following exception occurs:
Without passing the key to viewModel() I seem to get same instance for all. I've read that this has been fixed, but the solution was using NavHost, that I'm not using.
I'm using:
The text was updated successfully, but these errors were encountered: