Using ViewModel helps keep your app smooth by preserving UI data during screen rotations or other configuration changes. This avoids reloading data or restarting heavy tasks, which keeps frame rates stable around 60fps and reduces battery drain.
Why ViewModel survives configuration changes in Android Kotlin - Publishing Best Practices
To keep 60fps rendering, use ViewModel to hold UI data and avoid re-fetching or recalculating on every rotation. Combine ViewModel with LiveData or StateFlow to update UI efficiently without blocking the main thread.
ViewModel is part of Android Jetpack libraries, which add minimal size to your app (a few hundred KB). It does not increase startup time noticeably because it only creates or restores data when needed, not at app launch.
Android ViewModel is designed to survive configuration changes like rotations by tying data to the lifecycle of UI controllers. iOS uses different patterns like ViewControllers and state restoration APIs to handle similar cases. Android ViewModel is unique in its lifecycle-aware design.
Using ViewModel aligns with Android best practices recommended by Google Play. It helps avoid crashes or poor user experience during rotations, which can lead to negative reviews. Ensure your app handles lifecycle events gracefully to meet quality standards.
Your app takes 5 seconds to load this screen after rotation. What's likely wrong? You are probably reloading data or restarting tasks instead of using ViewModel to preserve state across configuration changes.
Practice
ViewModel survive configuration changes like screen rotations in Android?Solution
Step 1: Understand ViewModel lifecycle
A ViewModel is designed to be tied to the lifecycle of an activity or fragment but survives configuration changes like rotations.Step 2: Why it survives configuration changes
When a configuration change happens, the activity is destroyed and recreated, but the ViewModel instance is retained and reused.Final Answer:
Because it is tied to the lifecycle of the activity or fragment and not recreated on configuration changes -> Option AQuick Check:
ViewModel lifecycle = survives config changes [OK]
- Thinking ViewModel saves data to disk automatically
- Confusing ViewModel with static variables
- Assuming ViewModel runs on a background thread
Solution
Step 1: Identify correct ViewModelProvider usage
The correct syntax to get a ViewModel instance is using ViewModelProvider with the activity or fragment as owner.Step 2: Check Kotlin syntax
UsingViewModelProvider(this).get(MyViewModel::class.java)is the standard way to get the ViewModel instance.Final Answer:
val model = ViewModelProvider(this).get(MyViewModel::class.java) -> Option CQuick Check:
ViewModelProvider(this).get() = correct syntax [OK]
- Instantiating ViewModel directly with constructor
- Using incorrect ViewModelProvider syntax
- Confusing ViewModel class with instance
class MyViewModel : ViewModel() {
var counter = 0
}
// In Activity
val model = ViewModelProvider(this).get(MyViewModel::class.java)
model.counter += 1
println(model.counter)Solution
Step 1: Understand ViewModel data retention
The ViewModel instance survives configuration changes, so its data likecounteris preserved.Step 2: Trace counter increment
On first launch, counter is 0, incremented to 1 and printed. After rotation, the same ViewModel instance is used, counter is 1, incremented to 2 and printed.Final Answer:
1 on first launch, then increments to 2 after rotation -> Option BQuick Check:
ViewModel retains data = increments preserved [OK]
- Assuming ViewModel resets on rotation
- Thinking counter always starts at 0
- Expecting an error due to rotation
class MyViewModel : ViewModel() {
var data = "Hello"
}
// In Activity
val model = MyViewModel()
println(model.data)Solution
Step 1: Identify ViewModel instantiation error
ViewModel must be obtained via ViewModelProvider to survive configuration changes, not created with constructor.Step 2: Explain consequence
Direct instantiation means a new ViewModel is created every time, losing data on rotation.Final Answer:
ViewModel should not be instantiated directly; use ViewModelProvider instead -> Option DQuick Check:
Use ViewModelProvider, not constructor [OK]
- Creating ViewModel with constructor
- Thinking private variables fix lifecycle issues
- Blaming println for errors
Solution
Step 1: Identify best practice for preserving UI data
ViewModel is designed to hold UI-related data that survives configuration changes like rotations.Step 2: Choose correct data handling method
Storing user input in a ViewModel property and updating it on input changes keeps data safe and avoids unnecessary disk operations.Final Answer:
Store user input in a ViewModel property and update it on every input change -> Option AQuick Check:
ViewModel holds UI data safely across rotations [OK]
- Using SharedPreferences for temporary UI data
- Using static variables which can cause leaks
- Recreating ViewModel loses data
Ready to prove it?
Pass the test and this pattern moves unknown
