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.

SavedState integration with Koin viewmodels

See original GitHub issue

Is your feature request related to a problem? Please describe. Android introduced SavedState support in their ViewModels. Now we do not need to access onSaveInstanceState overrides from the Fragment / Activity. However Koin doesn’t agree with this even though we do have a technique for sending custom parameters in the viewmodels.

To access the saved state in the ViewModels , this will be the signature of the ViewModel

class SavedStateViewModel(val savedStateHandle: SavedStateHandle):ViewModel()
{
}

To create it without Koin , we use a ViewModelProvider ViewModelProviders.of(this,SavedStateVMFactory(this)).get(SavedStateViewModel::class.java)

Describe the solution you’d like

Describe alternatives you’ve considered I have tried injecting the SavedStateHandle by going through the following.

val viewModels = module {

    viewModel { (ssh:SavedStateHandle)-> SavedStateViewModel(ssh) }

}

This wont work because we need to access a different factory called SavedStateVMFactory in order for the SavedStateHandle to be sent into the ViewModel object

Is there anyway to achieve SavedStateHandles inside our viewmodels via Koin

https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate#kotlin

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tsuharesucommented, Jul 27, 2020

Having the same problem as @caiodev . I suspect that since my activity is the first, when Android kills it, it kills the whole app, including the handle. Might be an SavedStateHandle problem or Koin, I’m not sure at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android ViewModel & Navigation | Koin
The koin-android Gradle module introduces a new viewModel DSL keyword that comes in complement of single and factory, to help declare a ViewModel....
Read more >
Saved State module for ViewModel - Android Developers
When using this module, ViewModel objects receive a SavedStateHandle object through its constructor. This object is a key-value map that lets you write...
Read more >
Koin sharedViewModel with SavedStateHandle - Stack Overflow
I have single activity application and number of fragments. Some of these fragments are using my viewmodel, typically like this ...
Read more >
ViewModels with Saved State, Jetpack Navigation, Data ...
Using ViewModels in data-binding — Easy data-binding with ViewModels and LiveData; viewModelScope — Kotlin Coroutines and ViewModels integration ...
Read more >
Saving UI state with ViewModel SavedState and Dagger
Integrating SavedState ViewModel with Dagger and Hilt; Dagger Assisted Inject. Saving UI State — The Challenge. One of the main challenges in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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