How to prevent custom views from losing state across screen orientation changes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Android, an orientation change usually destroys and recreates the activity. If a custom view holds its own UI state, that state disappears unless the view participates in the normal save-and-restore mechanism. The correct fix is to save view-owned state in the view itself and keep screen-level data in a ViewModel or other higher-level state holder.
Use onSaveInstanceState and onRestoreInstanceState
A custom view should save its own transient UI state by overriding onSaveInstanceState() and onRestoreInstanceState().
This lets Android save and restore the custom view along with the rest of the view hierarchy.
Give the View a Stable ID
For view state restoration to work reliably, the custom view should have a stable ID in the layout.
Without an ID, the framework may not know how to match the restored state back to the recreated view instance.
Decide What State Belongs in the View
Not all state should live in a custom view. A good rule is:
- view-owned UI state belongs in the view's saved state
- screen or business state belongs in a
ViewModel, repository, or other higher layer
For example, the current scroll offset of a custom timeline view belongs in the view. A list of domain objects loaded from the database does not.
If you store too much in the view's saved state, restoration becomes fragile and hard to maintain.
Use a ViewModel for Screen-Level Data
If the custom view renders data that should survive rotation independently of the view hierarchy, keep that data in a ViewModel and rebind it when the new activity instance is created.
The custom view can still save lightweight presentation state such as selection or expansion position, while the ViewModel owns the underlying screen data.
Avoid configChanges as the Default Fix
Some developers try to stop recreation entirely by handling configuration changes manually in the manifest. That can be appropriate in special cases, but it is usually the wrong default for ordinary state loss in a custom view.
If the problem is that the view does not save its state correctly, fix the state-saving behavior. Do not bypass the lifecycle just to hide the bug.
Common Pitfalls
The most common mistake is storing custom view state in normal fields and expecting it to survive rotation automatically. It will not survive activity recreation unless you save and restore it.
Another issue is forgetting to call through to super.onSaveInstanceState() and super.onRestoreInstanceState(). That can break restoration higher up the view hierarchy.
Developers also often omit a stable view ID, which makes restoration unreliable.
Finally, avoid stuffing large domain objects into custom view state. Use a ViewModel for screen data and keep the view's saved state focused on UI behavior.
Summary
- Save custom-view UI state with
onSaveInstanceState()andonRestoreInstanceState(). - Use a
SavedStateclass derived fromAbsSavedState. - Give the view a stable ID in the layout.
- Keep view-owned UI state in the view and screen data in a
ViewModel. - Fix state saving directly instead of using
configChangesas a shortcut.

