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.

SavedStateHandle is not "reinjected" in a stateViewModel on system-initiated process death

See original GitHub issue

Describe 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:

  1. from AS, run app and start relative activity
  2. put app in background
  3. in AS, into logcat tab, click on Terminate App button (to simulate system-initiated process death)
  4. 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
arnaudgiulianicommented, Oct 24, 2022

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/1459

More 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 🙏

2reactions
arnaudgiulianicommented, Sep 7, 2022

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 👍

Read more comments on GitHub >

github_iconTop 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 >

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