Android & Jetpack Compose (50 Qs)•Part 1: Architecture, Jetpack Compose & Core Components (1 - 10)
What is a ViewModel in Android Architecture Components and why does it survive configuration changes?
senior LevelComponentsDocker & ContainersSystem Design
In Simple Words:
A ViewModel stores and manages UI-related data in a lifecycle-conscious way:
- Configuration Change Problem: Rotating the phone or switching to dark mode destroys and recreates the
Activity. Regular variables in the activity are lost! - ViewModel Lifecycle: The
ViewModelremains retained in memory in theViewModelStorethroughout the configuration change until the Activity is permanently finished (onDestroy()), preventing redundant network requests and UI re-fetching.