Android - prevent WebView from reloading when turning

When I rotate my screen, WebView reloads the entire page. I cannot do this because some of my materials contain dynamic / random material. Currently, when you rotate, the screen reloads the original URL from the loadUrl () method.

Any idea what is wrong with my code?

MainActivity.java

package com.mark.myapp; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { WebView web; String webURL = "http://www.google.co.uk/"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) ((WebView)findViewById(R.id.web)).restoreState(savedInstanceState); web = (WebView) findViewById(R.id.web); web.getSettings().setJavaScriptEnabled(true); web.loadUrl(webURL); web.setPadding(0, 0, 0, 0); web.getSettings().setLoadWithOverviewMode(true); web.getSettings().setUseWideViewPort(true); web.getSettings().setSupportZoom(true); web.getSettings().setBuiltInZoomControls(true); web.setWebViewClient(new HelloWebViewClient()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private class HelloWebViewClient extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView web, String url) { web.loadUrl(url); return true; } } public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { web.goBack(); return true; } return super.onKeyDown(keyCode, event); } } 

AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mark.myapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest> 
+72
java android android-webview
Aug 26 2018-12-12T00:
source share
11 answers

I think the main problem is that you are calling web.loadUrl (webURL); also while saving InstanceState! = null

EDIT

Try:

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

EDIT2 : You also need to override onSaveInstanceState and onRestoreInstanceState.

 @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 your AndroidManifest.xml to your Android activity: configChanges = "direction | screenSize". thank

+80
Aug 26 '12 at 14:50
source share

No java coding needed. use this in the manifest file.

  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 

as:

 <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> 
+60
Oct 24 '13 at 9:21
source share

in tag (manifest)

 android:configChanges="orientation|screenSize" 
+16
Jan 15 '15 at 15:48
source share

Adding android:configChanges="orientation|screenSize" to the manifest works for me

 <activity android:name="com.example.HelloWorld.WebActivity" android:label="@string/title_activity_web" android:configChanges="orientation|screenSize" > </activity> 
+12
Jul 14 '15 at 7:31
source share

I do not believe that this will work. In my case, restoring state using WebView.restore(Bundle savedInstanceState) still causes the URL to reload.

Looking at the docs for restoreState() , you will see that it says:

If it is called after this WebView has the ability to build state (load pages, create a list back / forth, etc.), there may be unwanted side effects.

and

Note that this method no longer restores display data for this WebView.

Substitutes for @ e4c5 to point me in the right direction in the answer

And, of course, the extreme course of action will be to prevent a change in orientation from starting or destroying activity. The documentation on how to do this is here

+7
Dec 09 '16 at 23:13
source share

Add this code to the manifest

 <activity ... android:configChanges="orientation|screenSize"> 
+5
Aug 25 '14 at 16:00
source share

Override the onConfigChange method to avoid reloading data when changing orientation

in your activity in the AndroidMainfest file.

 android:configChanges="orientation|keyboardHidden" 

as well as in your webview settings

 webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null); webview.loadUrl("Your URL To Load"); 
+4
Mar 18 '14 at 11:20
source share

Try this in the manifest file:

 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
+4
Oct 05 '17 at 11:12
source share

Put this in the action of the Manifest.xml file:

 android:configChanges="orientation|screenSize" 

Here is an example:

 <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" android:configChanges="orientation|screenSize"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 
+4
05 Oct '17 at 2:34 on
source share

As indicated here ,

Note: starting with Android 3.2 (API level 13), the screen size also changes when the device switches between portrait and landscape orientations. Thus, if you want to prevent reboots at runtime due to a change in orientation when developing for API level 13 or above (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the value “screenSize” in addition to the “orientation”, the cost. That is, you must decal the android: configChanges = "orientation | Screen size. "However, if your application targets API level 12 or lower, then your activity always processes this configuration change (this configuration change does not restart your activity, even when running on Android 3.2 or a higher device).

android:configChanges="orientation|screenSize" in your activity will solve this problem.

Also pay attention to the following

Remember: when you declare your activity to handle a change configuration, you are responsible for resetting any items for which you provide alternatives. If you declare that your activity handles orientation and image changes that landscape and portrait need, you must reassign each resource for each element during onConfigurationChanged ().

+4
Nov 21 '17 at 8:04 on
source share

This solution works well for me:

(1) In AndroidManifest.xml, add this android line: configChanges = "keyboard | hidden keyboard | orientation | screenLayout | uiMode | screenSize | smalllestScreenSize"

How is it (and as the answers above)

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

(2) Then in MainActivity.java, confirm the saved_instance

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainContext = getApplicationContext(); ---- myWebView = (WebView) findViewById(R.id.webView); prepareWebView(); myWebView.addJavascriptInterface(myJavaScriptInterface, "WEB2Android"); if (savedInstanceState == null) { myWebView.post(new Runnable() { @Override public void run() { myWebView.loadUrl("http://www.appbiz.ro/foto_konta"); } }); } ---- } 

(3) and then:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } @Override protected void onSaveInstanceState(Bundle outState ) { super.onSaveInstanceState(outState); myWebView.saveState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); myWebView.restoreState(savedInstanceState); } 
+3
Aug. 27 '18 at 2:54
source share



All Articles