Android WebView Handling Orientation Changes: Saving State

🎯 Overview

Android WebView is a component that allows developers to display web content within their applications. When the device undergoes orientation changes (e.g., from portrait to landscape or vice versa), the Activity is recreated, and any unsaved data can be lost. To prevent data loss and maintain the WebView's state during such changes, developers can save and restore the WebView state using the onSaveInstanceState and restoreState methods.

The concept behind handling orientation changes for Android WebView involves saving the state of the WebView before the Activity is destroyed and restoring that state when the Activity is recreated. This ensures a seamless user experience, as the web page displayed in the WebView will remain consistent across orientation changes.

🎯 Basic Syntax

To save the state of the WebView, the onSaveInstanceState method must be overridden in the Activity's code. It is responsible for saving relevant data into a Bundle object. The WebView's saveState method is used to store the WebView's state into the Bundle. On the other hand, to restore the state, the restoreState method is called on the WebView object, passing the Bundle containing the saved state.

🎯 Sample Code

// WebView Declaration:

WebView WebBrowser;

// Code for WebView SaveState Mapping

if (savedInstanceState != null)




// SaveInstanceState Method

protected void onSaveInstanceState(Bundle outState) {



public void onCreate(final Bundle savedInstanceState) {



    WebBrowser = (WebView) findViewById(;


🎯 Implementation

The implementation of WebView state handling involves the following steps:

🎯 Detailed Explanation

🎯 Key Points