Member-only story
Ever wondered how your Android app retains user data even when an Activity is destroyed during a screen rotation? What is the internal working of the ViewModel??
5 min read 1 hour ago
This question isn’t just a technical detail — it’s one of the most crucial and frequently asked interview questions in Android development. Dive in as we unravel the secret behind how the ViewModel persists data through configuration changes, ensuring a seamless user experience even when the UI components are rebuilt!
ViewModel is an Android Architecture component that manages UI-related data and persists the data during configuration changes (such as screen orientation changes).
Key Points:
1. Lifecycle Awareness:
🔹The ViewModel is tied to the lifecycle of the activity/fragment but is separate from the UI components that are re-created during configuration changes (such as screen rotation).
🔹A new Activity instance is created when the orientation changes, but the ViewModel instance is not destroyed. This keeps your UI data intact.
2. Data Persistence Across Configuration Changes:
🔹The Android system stores ViewModels in a special…