Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Narayane opened this issue Jul 18, 2022 · 6 comments

Comments

@Narayane
Copy link

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
@arnaudgiuliani
Copy link
Member

thanks for you feedback

@arnaudgiuliani
Copy link
Member

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 👍

@Narayane
Copy link
Author

Narayane commented Sep 8, 2022

Thanks :)

@Rayhan-kh
Copy link

On app "restoration", getting null for SavedStateHandle attributes

private val viewModel: PaxViewModel by stateViewModel(owner = {
        findNavController().getViewModelStoreOwner(
            R.id.pax_graph
        )
    })
class PaxViewModel(private val stateHandle: SavedStateHandle) : ViewModel() {

    var surName: String = stateHandle["surName"] ?: ""
    var givenName: String = stateHandle["givenName"] ?: ""

    init {
        Log.d("surName", stateHandle["surName"] ?: "")
        Log.d("givenName", stateHandle["givenName"] ?: "")
    }

    fun saveState() {
        stateHandle["surName"] = surName
        stateHandle["givenName"] = givenName
    }

    fun clearSavedState() {
        stateHandle["surName"] = null
        stateHandle["givenName"] = null
    }
}

Koin project used and used version (please complete the following information):

  • koin-core version 3.2.0
    androidx.lifecycle:lifecycle-viewmodel-savedstate version 2.5.1

@mrmike
Copy link

mrmike commented Sep 20, 2022

Google has reworked SavedState instance creation API and how it's passing around for ViewModels.

Could you elaborate more on what has changed?

Thanks

@arnaudgiuliani arnaudgiuliani modified the milestones: 3.3.0, android-3.3.0 Oct 24, 2022
@arnaudgiuliani
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants