Alexander Mitropolsky
11/11/2021, 5:00 PMscope(qualifier<SomeFragment>())
scope(qualifier<LongLiveScope>()) {
viewModel { SomeViewModel ()}
}
Then we link these scopes:
fragmentScope.linkTo(longLiveScope)
Then we inject SomeViewModel into SomeFragment with the viewModel()
extension, which uses the ViewModelStore from SomeFragment.
When SomeFragment is destroyed, its ViewModelStore is also destroyed, and SomeViewModel::onClear
will be called.
However, the SomeViewModel instance will not be destroyed because it is scoped in LongLiveScope, which leads to issues when the same (already cleared) instance of SomeViewModel is reused in new fragments. For example, ViewModel’s coroutines scope will not work because it will be already closed after onCleared.
I think the ViewModel should be shared using the shared ViewModelStore, but not using the Koin scope, otherwise, it breaks the lifecycle of the ViewModel itself.
I hope I explained this issue in sufficient detail.
Can you please explain why the ViewModel definition has changed?
And what can I do if the ViewModelStoreOwner is different from the scope, other than rolling back the old ViewModel definition?
Thank you!arnaud.giuliani
11/12/2021, 8:17 AMarnaud.giuliani
11/12/2021, 8:17 AMarnaud.giuliani
11/12/2021, 8:18 AM