Android WebView: handling orientation changes without losing state
Codemia Knowledge Hub ·
When an Android device rotates, the activity is usually destroyed and recreated, which can reset a WebView's page state, scroll position and in-progress loading. Rather than suppressing rotation blindly, decide whether to restore WebView state across recreation or handle configuration changes yourself. Saving and restoring the state is usually the cleaner option.
Save and restore WebView state
WebView has built-in state persistence through saveState and restoreState:
Start here, because it works with the normal Android lifecycle instead of fighting it.
Saving only the URL loses history and form state
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 does.
Use configChanges only deliberately
You can prevent activity recreation with manifest flags:
Then handle the change manually:
This can reduce reloads, but it shifts more responsibility to your activity. Do it only when you genuinely want to own configuration handling.
Configure the WebView before restoring
Some bugs blamed on rotation are really incomplete WebView configuration. Set the WebView up the same way before any restore or reload:
Without a WebViewClient, links may open in an external browser. Without the required settings, the restored page may behave differently from the original.
Retaining a heavy WebView across recreation
Because WebView is relatively expensive, some apps retain it across recreation with more elaborate patterns. That can work, but it adds lifecycle complexity and the risk of leaking an activity context. For many apps, saveState and restoreState are enough, so try them before building a retention strategy.
Test with real navigation before rotating
Verify actual 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.
Mistakes to avoid
- 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 lives in page-side JavaScript or server-side session logic.
Related reading
- Android webview launches browser when calling loadurl
- Android webview localStorage
- Android what does the clipToPadding Attribute do?
- Android When is onCreateOptionsMenu called during Activity lifecycle?
- Android When should I use a Handler and when should I use a Thread?
- Android Why can't I create a handler in new thread
- Android why is there no maxHeight for a View?
- Android XML Percent Symbol
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.