When a ViewModel is recreated in Jetpack Compose, your screen state resets. Your logic may also run again. This often happens during configuration changes such as screen rotation.
The Problem
Imagine a simple screen with a counter and a button. Each time the button is pressed, the counter increases.
However, when a configuration change happens, such as device rotation, the counter suddenly resets back to zero.
This happens because the ViewModel is recreated.
Watch the Video Tutorial
Visual Explanation
If the ViewModel is recreated, the state stored inside it is also recreated. That means the counter returns to its initial value.
How to Detect the Issue
One simple way to confirm this behavior is by printing the memory address of the ViewModel in Logcat.
Each time the ViewModel is initialized, the memory address will be printed.
If the memory address changes after a configuration change, that means a new ViewModel instance was created.
The Incorrect Implementation
A common mistake is manually creating the ViewModel inside a composable.
val viewModel = ProfileViewModel()
Log.d("ProfileScreen", "viewModel: $viewModel")
// UI Content
This creates a new ViewModel every time the composable is recreated.
When configuration changes occur, Compose recomposes the screen and the ViewModel gets initialized again.
The Correct Fix
Instead of creating the ViewModel manually, use the lifecycle-aware viewModel() function.
val viewModel : ProfileViewModel = viewModel()
Log.d("ProfileScreen", "viewModel: $viewModel")
// UI content
This attaches the ViewModel to the lifecycle owner.
Because of this, the ViewModel survives configuration changes and recompositions.
What Happens Now
After applying this fix:
- The ViewModel is created only once
- The memory address stays the same
- The state persists across configuration changes
Testing the Fix
Try the following steps in your app.
- Increment the counter
- Rotate the device
- Check the counter value
If the ViewModel is correctly scoped, the counter will keep its value.
Logcat will also show the same ViewModel memory address.
Key Takeaway
If you manually create a ViewModel inside a composable, it will be recreated during recomposition.
Always use the lifecycle-aware ViewModel provider.
val viewModel: CounterViewModel = viewModel() This ensures the ViewModel survives configuration changes and your state remains stable.
Conclusion
Jetpack Compose recompositions can easily recreate objects if they are not lifecycle-aware.
Using the correct ViewModel initialization prevents state loss and keeps your UI stable.
If you are building Compose apps, understanding this pattern will save you from many subtle bugs.
Follow Coding Reel for more practical Android development tips and fixes.
No comments:
Post a Comment
Share your thoughts ...