How can I save an activity state using the save instance state?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
onSaveInstanceState is Android’s built-in way to preserve small pieces of UI state across activity recreation, especially during configuration changes such as rotation. The key idea is to save lightweight values into a Bundle and restore them when the activity is created again.
Save State in onSaveInstanceState
Override onSaveInstanceState and write the values you need into the outgoing Bundle.
This method is for transient UI state, not for large datasets or long-term persistence.
Restore in onCreate or onRestoreInstanceState
When the activity is recreated, the saved bundle is passed back. You can restore from onCreate after setContentView, or in onRestoreInstanceState if you want a dedicated restore point.
That is enough for many common rotation and temporary-process-recreation scenarios.
Save Only What You Need to Rebuild the UI
A Bundle should contain lightweight reconstruction data such as:
- text field contents
- selected tab or item ID
- checkbox state
- current page number
- simple parcelable objects
It should not hold large bitmaps, full database rows, or network responses. Those belong in a database, file cache, repository, or ViewModel-backed source.
Use Parcelable for Custom Lightweight Objects
If you must store a custom object, prefer Parcelable over Serializable for Android-specific state passing.
Use this only when the object is small and directly relevant to recreating the current screen.
Know What onSaveInstanceState Does Not Solve
This mechanism is not a replacement for durable persistence. If the data must survive process death permanently, app restarts, or user logout, save it somewhere durable.
It is also not the ideal tool for larger screen-state models. For that, ViewModel is often cleaner because it survives configuration changes without constantly serializing everything into a Bundle.
Combine with ViewModel When State Gets Richer
For richer screens, use ViewModel for in-memory state and onSaveInstanceState only for the minimum needed to reconnect the recreated activity to that state.
That hybrid approach avoids overloading the bundle while still surviving lifecycle events correctly.
Test Real Recreation Paths
Do not assume state saving works just because the code compiles. Rotate the device, change dark mode, and test process recreation with developer options if the screen is important. Android lifecycle bugs are usually about missing restore behavior, not about syntax.
A good test question is simple: if Android destroyed and recreated the screen right now, what minimum values would I need to rebuild the same user-visible state? That is usually the exact boundary for what belongs in the bundle.
Common Pitfalls
- Saving too much data in the
Bundleand treating it like a general persistence layer. - Forgetting to restore values after the activity is recreated.
- Storing large objects that should live in a repository, database, or file cache instead.
- Assuming
onSaveInstanceStateis a substitute forViewModelin richer screen-state designs. - Testing only the happy path and never verifying real activity recreation scenarios.
Summary
- Use
onSaveInstanceStatefor small UI state needed after activity recreation. - Restore from the provided
BundleinonCreateoronRestoreInstanceState. - Keep saved values lightweight and reconstruction-oriented.
- Use
Parcelablefor small custom objects when necessary. - Prefer
ViewModelor durable storage for larger or longer-lived state.

