SavedStateHandle is not "reinjected" in a stateViewModel on system-initiated process death
See original GitHub issueDescribe the bug StateViewModel SavedStateHandle attribute is always null after a system-initiated process death
class InsuranceOnboardingWorkflowActivity : BaseActivity() {
private val viewModel by stateViewModel<InsuranceOnboardingWorkflowActivityViewModel>(state = { intent.extras!! })
companion object {
fun getIntent(context: Context, source: AppSource, insuranceId: String? = null): Intent {
return Intent(context, InsuranceOnboardingWorkflowActivity::class.java).apply {
putExtra(InsuranceOnboardingWorkflowActivityViewModel.EXTRA_SOURCE, source.name)
insuranceId?.let {
putExtra(InsuranceOnboardingWorkflowActivityViewModel.EXTRA_INSURANCE_ID, it)
}
}
}
}
val insuranceModule = module {
viewModel { InsuranceOnboardingWorkflowActivityViewModel(get(), get(), get(), get(), get()) }
class InsuranceOnboardingWorkflowActivityViewModel(
stateHandle: SavedStateHandle,
internal val logRepository: LogRepository,
internal val insuranceRepository: InsuranceRepository,
internal val premiumRepository: PremiumRepository,
internal val userRepository: UserRepository
) : LRViewModel() {
var source: AppSource = AppSource.valueOf(stateHandle.get<String>(EXTRA_SOURCE)!!)
To Reproduce Steps to reproduce the behavior:
- from AS, run app and start relative activity
- put app in background
- in AS, into logcat tab, click on Terminate App button (to simulate system-initiated process death)
- onto relative device, click on app “window” in “app carrousel” to “restore” app
Expected behavior On app “restoration”, viewmodel SavedStateHandle attribute should not be null
Koin project used and used version (please complete the following information):
koin-core version 3.1.2
androidx.lifecycle:lifecycle-viewmodel-savedstate version 2.4.1
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Viewmodel SavedStateHandle data lost after process death
My simple case is that I'm just storing an Int value in the SavedStateHandle and trying to get it again after the process...
Read more >Saved State module for ViewModel - Android Developers
By using SavedStateHandle , the query value is retained across process death, ensuring that the user sees the same set of filtered data...
Read more >Issues · InsertKoinIO/koin · GitHub
SavedStateHandle is not "reinjected" in a stateViewModel on system-initiated process death android important status:accepted accepted to be developed ...
Read more >SavedStateHandle is not "reinjected" in a stateViewModel on system ...
Describe the bug StateViewModel SavedStateHandle attribute is always null after a system-initiated process death class InsuranceOnboardingWorkflowActivity ...
Read more >Android ViewModels: Saving State across Process Death
Handling your state via ViewModels solves this issue. Using SavedStateHandle. Add lifecycle-viewmodel-savedstate dependency dependencies { def lifecycle_version ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The VIewModel API has been revamped with the latest lifecycle library in 2.5.1. This implies that
SavedStateHandle
is created with the right extras (thanks to the lifecycle library update in 2.5.1). You can have a look to the full PR https://github.com/InsertKoinIO/koin/pull/1459More details on Google API: https://developer.android.com/jetpack/androidx/releases/lifecycle#2.5.1
Don’t hesitate to give feedback on
koin-android
3.3.0 🙏Google has reworked SavedState instance creation API and how it’s passing around for ViewModels.
Koin 3.3.0 will bring some new API for this. I postpone for
3.3.0
then 👍