Android - Preventing WebView reload on Rotate

mark picture mark · Aug 26, 2012 · Viewed 69.7k times · Source

When I rotate my screen, the WebView reloads the whole page. I can't have this since some of my content contains dynamic/random material. Currently when rotated the screen reloads the original URL from the loadUrl() method.

Any idea what's wrong with my code?

MainActivity.java

Answer

Giuseppe picture Giuseppe · Aug 26, 2012

I think the main problem is that you call web.loadUrl(webURL); also when savedInstanceState != null

EDIT

Try:

if (savedInstanceState == null)
{
  web.loadUrl(webURL);
}

EDIT2: You also need the onSaveInstanceState and onRestoreInstanceState override.

@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}

Note: Please also add in your AndroidManifest.xml in your Activity android:configChanges="orientation|screenSize" Thanks