Android
WebView
Orientation Changes
Mobile Development
Android Development

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:

kotlin
1class BrowserActivity : AppCompatActivity() {
2
3    private lateinit var webView: WebView
4
5    override fun onCreate(savedInstanceState: Bundle?) {
6        super.onCreate(savedInstanceState)
7        setContentView(R.layout.activity_browser)
8
9        webView = findViewById(R.id.webView)
10        webView.settings.javaScriptEnabled = true
11        webView.webViewClient = WebViewClient()
12
13        if (savedInstanceState != null) {
14            webView.restoreState(savedInstanceState)
15        } else {
16            webView.loadUrl("https://example.com")
17        }
18    }
19
20    override fun onSaveInstanceState(outState: Bundle) {
21        super.onSaveInstanceState(outState)
22        webView.saveState(outState)
23    }
24}

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:

kotlin
outState.putString("url", webView.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:

xml
<activity
    android:name=".BrowserActivity"
    android:configChanges="orientation|screenSize" />

Then handle the change manually:

kotlin
override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
}

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:

kotlin
webView.settings.javaScriptEnabled = true
webView.webViewClient = WebViewClient()

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 configChanges to suppress recreation without understanding the tradeoffs.
  • Recreating a heavy WebView repeatedly when state restoration would have been enough.
  • Forgetting basic setup such as WebViewClient and required settings.
  • Treating all lost state as an orientation problem when some of it lives in page-side JavaScript or server-side session logic.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms