Internals of a ViewModel — Part 3
Explaining how the ViewModel gets saved during config changes
Internals of ViewModel: How it Survives Configuration Changes in Android
When developing Android applications, one of the most common challenges is managing UI-related data during configuration changes, such as screen rotations. The ViewModel class, introduced as part of Android Jetpack, provides a solution by retaining data during these changes.
But What’s behind the scenes of a ViewModel? Let’s dive into the internals and understand how a ViewModel survives configuration changes.
The Secret Behind ViewModel Retention: ViewModelStore
The ViewModelStore is at the core of how ViewModel instances persist during configuration changes. It acts as a container that stores and retrieves ViewModel instances for the Android components (like Activities or Fragments) that they are scoped to.
Breaking Down ViewModelStore
At its heart, the ViewModelStore is essentially a simple map with the following structure:
Key: A unique String identifier for each ViewModel.
Value: The actual ViewModel instance.
The ViewModelStore is responsible for holding onto ViewModel instances during configuration changes and ensuring they are correctly re-associated with the new instance of the Activity or Fragment.
How ViewModel Works with ViewModelStore
When an Activity or Fragment is destroyed and recreated (e.g., due to a screen rotation), the ViewModelStore retains the existing ViewModel instances. These instances are then reconnected to the new Activity or Fragment.
Here’s how the process works step-by-step:
Saving the ViewModel:
When a ViewModel is created for the first time, it is stored in the ViewModelStore using the put() function:
Retrieving the ViewModel:
After a configuration change, the system retrieves the existing ViewModel from the ViewModelStore using the get() function:
Clearing the ViewModel:
When the associated component (Activity or Fragment) is permanently destroyed, the ViewModelStore clears its stored ViewModel instances using the clear() function
Lifecycle Integration
The ViewModelStore is tightly integrated with the lifecycle of Android components. Each ViewModel is scoped to its associated LifecycleOwner (e.g., an Activity or Fragment).
- On Configuration Change:
- The ViewModelStore persists in ViewModel instances as the LifecycleOwner is recreated.
- On Permanent Destruction:
- When the LifecycleOwner is finished (e.g., when the user exits the app or closes the Activity), the ViewModelStore clears all stored ViewModel instances.
Why Use ViewModelStore?
The ViewModelStore provides several benefits:
- Data Retention: Ensures that UI-related data survives configuration changes without the need for complex manual handling.
- Memory Efficiency: Prevents unnecessary recreation of ViewModel instances, saving resources.
- Ease of Use: Provides a simple API for storing, retrieving, and clearing ViewModel instances.
If you want to know more about the internals of a ViewModel. Here is a video for you:
ViewModelStoreOwner: Connecting the Dots
The ViewModelStoreOwner interface is implemented by Android components like Activities and Fragments. This interface provides access to the ViewModelStore for each component:
When you use the ViewModelProvider to create or retrieve a ViewModel, it interacts with the ViewModelStore provided by the ViewModelStoreOwner.
Conclusion
The ViewModel’s ability to survive configuration changes is powered by the ViewModelStore, which acts as a repository for storing and retrieving ViewModel instances. By leveraging a simple map structure and lifecycle-aware integration, the ViewModelStore ensures that your UI-related data remains intact and easily accessible.
