The current ViewModel being managed by this scenario. This instance is change if the ViewModelStore is cleared or if recreate is invoked.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-07-30 UTC.
[null,null,["Last updated 2025-07-30 UTC."],[],[],null,["# ViewModelScenario\n=================\n\nArtifact: [androidx.lifecycle:lifecycle-viewmodel-testing](/jetpack/androidx/releases/lifecycle) \n[View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/testing/ViewModelScenario.kt+class:androidx.lifecycle.viewmodel.testing.ViewModelScenario) \nAdded in [2.9.0](/jetpack/androidx/releases/lifecycle#2.9.0)\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario \"View this page in Java\")\nCommon/All\n - Cmn \n\n ```\n class ViewModelScenario\u003cVM : ViewModel\u003e\n ```\n\n*** ** * ** ***\n\n[ViewModelScenario](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario) provides API to start and drive a [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel)'s lifecycle state for testing.\n\n[ViewModelScenario.recreate](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario#recreate()) allows you to simulate a System Process Death and restoration.\n\n[ViewModelScenario](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario) does not clean up the [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) automatically. Call [close](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario#close()) in your test to clean up the state or use [AutoCloseable.use](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/index.html) to ensure [ViewModelStore.clear](/reference/kotlin/androidx/lifecycle/ViewModelStore#clear()) and [ViewModel.onCleared](/reference/kotlin/androidx/lifecycle/ViewModel#onCleared()) is invoked.\n\nSummary\n-------\n\n| ### Public functions |\n|--------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|\n| `open `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [close](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario#close())`()` Finishes the managed [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) and clear the [ViewModelStore](/reference/kotlin/androidx/lifecycle/ViewModelStore). | Cmn |\n| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [recreate](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario#recreate())`()` Simulates a System Process Death recreating the [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) and all associated components. | Cmn |\n\n| ### Public properties |\n|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|\n| `VM` | [viewModel](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario#viewModel()) The current [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) being managed by this scenario. | Cmn |\n\nPublic functions\n----------------\n\n### close\n\nCmn \nAdded in [2.9.0](/jetpack/androidx/releases/lifecycle#2.9.0) \n\n```\nopen fun close(): Unit\n```\n\nFinishes the managed [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) and clear the [ViewModelStore](/reference/kotlin/androidx/lifecycle/ViewModelStore). \n\n### recreate\n\nCmn \nAdded in [2.9.0](/jetpack/androidx/releases/lifecycle#2.9.0) \n\n```\nfun recreate(): Unit\n```\n\nSimulates a System Process Death recreating the [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) and all associated components.\n\nThis method:\n\n- Saves the state of the [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) with [SavedStateRegistryController.performSave](/reference/kotlin/androidx/savedstate/SavedStateRegistryController#performSave(androidx.savedstate.SavedState)).\n\n- Recreates the [ViewModelStoreOwner](/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner), [LifecycleOwner](/reference/kotlin/androidx/lifecycle/LifecycleOwner) and [SavedStateRegistryOwner](/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner).\n\n- Recreates the [ViewModelProvider](/reference/kotlin/androidx/lifecycle/ViewModelProvider) instance.\n\nCall this method to verify that the [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) correctly preserves and restores its state.\n\nPublic properties\n-----------------\n\n### viewModel\n\nCmn \nAdded in [2.9.0](/jetpack/androidx/releases/lifecycle#2.9.0) \n\n```\nval viewModel: VM\n```\n\nThe current [ViewModel](/reference/kotlin/androidx/lifecycle/ViewModel) being managed by this scenario. This instance is change if the [ViewModelStore](/reference/kotlin/androidx/lifecycle/ViewModelStore) is cleared or if [recreate](/reference/kotlin/androidx/lifecycle/viewmodel/testing/ViewModelScenario#recreate()) is invoked."]]