Android WebView handling orientation changes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When an Android device rotates, the activity is usually destroyed and recreated. That default behavior is fine for many screens, but it can be painful for a WebView because page state, scroll position, and in-progress loading can all be affected.
The right fix is usually not to suppress rotation blindly. Instead, decide whether you want to restore WebView state across recreation or deliberately handle configuration changes yourself. In most cases, saving and restoring the WebView state is the cleaner option.
Save and Restore WebView State
WebView already supports built-in state persistence through saveState and restoreState:
This is usually the best first solution because it works with the normal Android lifecycle instead of fighting it.
Saving Only the URL Is Less Complete
A common shortcut is saving just the URL:
That is better than nothing, but it does not preserve browsing history, form content, or as much internal WebView state as saveState. If the goal is a smooth user experience, the built-in state bundle is usually more complete.
Use configChanges Only Deliberately
Some developers prevent activity recreation with manifest flags:
Then handle the change manually:
This can reduce reloads, but it also shifts more responsibility to your activity. It is usually worth doing only when you genuinely want to own configuration handling yourself.
Keep the WebView Setup Correct
Orientation bugs are often blamed on rotation when the real issue is incomplete WebView configuration. At minimum, make sure the WebView is set up consistently before any restore or reload happens:
Without a WebViewClient, links may open in an external browser. Without required settings, the restored page may behave differently from the original page.
Heavy WebViews and Lifecycle Tradeoffs
Because WebView is relatively expensive, some apps try to retain it across recreation using more elaborate patterns. That can work, but it also increases lifecycle complexity and the risk of leaking an activity context.
For many apps, saveState and restoreState are enough. Start there before building a more complicated retention strategy.
Test with Real Navigation Behavior
State restoration is easier to trust when you verify actual user behavior after rotation: page history, form input, scroll position, and in-page navigation. A simple URL reload can hide bugs that only appear when users interact with the page before rotating.
Common Pitfalls
- Saving only the URL and assuming that restores the full browsing state.
- Using
configChangesto suppress recreation without understanding the tradeoffs. - Recreating a heavy WebView repeatedly when state restoration would have been enough.
- Forgetting basic setup such as
WebViewClientand required settings. - Treating all lost state as an orientation problem when some of it actually lives in page-side JavaScript or server-side session logic.
Summary
- Android normally recreates activities on orientation changes, which affects
WebView. - The clean default fix is
saveStateplusrestoreState. - Saving only the URL is usually less complete than saving full WebView state.
- Handle
configChangesmanually only when you truly want to own rotation behavior. - Start with correct WebView configuration before blaming orientation handling alone.

