Use ViewModels as implementation details of a screen-level state holder, not for reusable UI components like chip groups or forms, which would share an instance under the same ViewModelStoreOwner. A ViewModel remains in memory until its ViewModelStoreOwner disappears: an activity finishes, a Navigation entry leaves the back stack, or a composable exits the Composition. Request a ViewModel when the system first calls onCreate(); it persists UI state through configuration changes like screen rotation until the activity is finished and destroyed. Override onCleared to clean up dependencies when the ViewModelStoreOwner destroys it. Use viewModelScope, a built-in CoroutineScope following the ViewModel's lifecycle, for business-related operations. From lifecycle version 2.5, pass Closeable objects to the constructor for automatic closing on clearance. Scope: Android Kotlin. Reviewed October 4, 2026.
ViewModel and app architecture
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- developer.android.com
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.