Koin: SavedState integration with Koin viewmodels

Created on 19 Jun 2019  路  12Comments  路  Source: InsertKoinIO/koin

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

duplicated

Most helpful comment

It seems to be ready: https://doc.insert-koin.io/#/koin-android/viewmodel?id=viewmodel-and-state-bundle

All 12 comments

won't fix as already in proposal for development

any update ?

up, any news?

It seems to be ready: https://doc.insert-koin.io/#/koin-android/viewmodel?id=viewmodel-and-state-bundle

The thing is to know if this is the good approach to let use the SavedState bundle
I will publish a poll about that

Tried to use koin-androidx-viewmodel:2.1.0-alpha-10 as mentioned in this link - https://doc.insert-koin.io/#/koin-android/viewmodel?id=viewmodel-and-state-bundle but gives me an import error for stateViewModel()

Just an update. I used koin-androidx-viewmodel:2.1.0-beta-1 and could use stateViewModel(). However I have a question - will the saved state handle work when app is killed and restarted?

will the saved state handle work when app is killed and restarted?

check the Android API for that. We are just passing the right instance around

Just an update. I used koin-androidx-viewmodel:2.1.0-beta-1 and could use stateViewModel(). However I have a question - will the saved state handle work when app is killed and restarted?

I'm having a problem with that in version 2.1.6. When i first open my app, kill it simulating a System initiated process death(adb shell am kill ) and restore it, all the data is gone. But if i repeat this process, it works.

It's like if koin after the first app death returned me the default bundle i pass to it at the beginning and only on the second time doing it, it will return me the stored instance. In my case, a list that i use to populate a recyclerview again in case of process death. I'm following the instructions described here: https://doc.insert-koin.io/#/koin-android/viewmodel?id=viewmodel-and-state-bundle. Have you ever seen something like this?

To use stateViewModel I am using the following implementation settings in my Gradle file:

implementation 'org.koin:koin-android:2.1.6'
implementation 'org.koin:koin-androidx-viewmodel:2.1.6'

Btw: Can someone tell me whats the difference between _org.koin:koin-androidx-viewmodel:2.1.6_ and _org.koin:koin-android-viewmodel:2.1.6_? I mean why is stateViewModel not included in the last one?

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.

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.

I've actually changed the way I was doing it. Now I'm using Room instead of SavedStateHandle. It's more reliable and you can store way more data into it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iRYO400 picture iRYO400  路  3Comments

luna-vulpo picture luna-vulpo  路  4Comments

CristianMG picture CristianMG  路  3Comments

sankarsana picture sankarsana  路  4Comments

fmobus picture fmobus  路  4Comments