17

I have a simple WebView that runs a web application on an Android. The problem is when I rotate the phone to change it to landscape the webview reloads and goes back to the beginning.

How can I prevent this action?

Ron

Fat Monk
  • 2,077
  • 1
  • 26
  • 59
RonRasmussen
  • 171
  • 1
  • 1
  • 4

7 Answers7

52

Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare

<activity android:configChanges="orientation|screenSize">

Excerpt from Handle configuration changes (webarchive to original page)

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Eli
  • 600
  • 6
  • 4
7

This can be resolved by overriding onSaveInstanceState(Bundle outState) in your activity and calling saveState from the Webview:

This blog post may be of help to you.

Emil
  • 7,220
  • 17
  • 76
  • 135
mohammadcode
  • 103
  • 9
6

Add the following to your AndroidManifest:

android:configChanges="orientation|keyboard|keyboardHidden"

So it should look something like this:

<activity android:name=".MyActivity" 
          android:label="@string/app_name" 
          android:configChanges="orientation|keyboard|keyboardHidden">

Obviously, if your WebView needs keyboard support then don't include the keyboard options.

Han
  • 5,374
  • 5
  • 31
  • 31
3

Add this before the oncreate

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

Write the oncreate this way. put final

public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutname);
if (savedInstanceState != null)
{
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
else
{
webview.loadUrl("http://www.playbuzz.org/");
}

}

In Androidmanifest insert under the activity

android:configChanges="keyboardHidden|orientation"
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
2
@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
} 

Add above method in your activity.. and

android:configChanges="keyboard|keyboardHidden|orientation"

in your manifest file

sandy goria
  • 144
  • 5
  • The question wasn't how to enable orientation change, but how to prevent reload on orientation change. – drogon Nov 08 '12 at 15:51
1

Add in Activity

android:configChanges="keyboardHidden|orientation"
1

The problem with the above solution is that it renders the screen white for some time before the webview redraws the contents.

Try using android:configChanges="orientation|keyboardHidden" inside your AndroidManifest.xml file for the activity that displays the webview. That should help.

soumyajit das
  • 131
  • 1
  • 7