Android - Preventing WebView reload on Rotate
in tag (manifest)
android:configChanges="orientation|screenSize"
EDIT: Found a more suitable solution.
NEW:
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Example.WebviewSample">
<application
<activity android:name=".WebViewActivity"
<!-- ADD THIS -->
android:configChanges="orientation|screenSize">
</activity>
</application>
</manifest>
In WebViewActivity.java
file add this to onCreate()
method
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
wView = (WebView) findViewById(R.id.webView);
if (savedInstanceState == null) {
wView.loadUrl("https://google.com");
}
}
also update onSaveInstanceState
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
wView.saveState(outState);
}
and onRestoreInstanceState
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
wView.restoreState(savedInstanceState);
}
OLD:
No java coding needed. use this in your manifest file.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
like:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.Example.WebviewSample.webviewsample"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
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