Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Webview Reloads When I Change From Portrait To Landscape

Tags:

webview

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

like image 269
RonRasmussen Avatar asked Apr 12 '11 00:04

RonRasmussen


People also ask

How do I change the orientation of Webview in android?

All you need is add android:configChanges="orientation|screenSize" in manifest.

How do I change the orientation of my apps?

Android Settings Start by going to Settings => Display and locate the “Device rotation” setting. On my personal cell phone, tapping this will reveal two options: “Rotate the contents of the screen,” and “Stay in portrait view.”


4 Answers

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.

like image 20
mohammadcode Avatar answered Oct 21 '22 08:10

mohammadcode


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">

Here's the docs: http://developer.android.com/guide/topics/resources/runtime-changes.html

like image 75
Eli Avatar answered Oct 21 '22 08:10

Eli


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.

like image 21
Han Avatar answered Oct 21 '22 08:10

Han


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"
like image 42
Cristiana Chavez Avatar answered Oct 21 '22 08:10

Cristiana Chavez