WebView is reloading when app is gone background and came back in iOS and Android?


I've a small app which holds a webview nothing more than that. I'm loading the webview with a url. but when my app is gone background and came back the webview is reloading, For me it should not happen. How to stop webview to reload when app is going to background in iOS and Android.

<strong>Note:</strong> <em>Both safari(iOS) and chrome(Android) are handling that event with same url even when they've gone background. They are not reloading the webview or data.</em>

This is my code, do I need to add anything more:


NSURL* url = [NSURL URLWithString:ip]; [webView loadRequest:[NSURLRequest requestWithURL:url]];


String url = "http://"+ip+":8081"; webView.loadUrl(url);


There are several reasons for the reloading of the App.


If you put your code in onCreate() method whenever the Activity goes to stop state and if the device memory is less then the process is killed and memory is allocated to the high priority app. while coming back it resumes from the oncreate() method, here not only your webView but entire app is reloaded.

And if your activity is in stop state and the user navigates it then it starts from onRestart() where it will not touch the onCreate but goes through onStart() where your app will not restart but the things you put in onstart and onResume.

Basically you have to understand the Activity states for this, Here is a link. <a href="https://developer.android.com/reference/android/app/Activity.html" rel="nofollow">https://developer.android.com/reference/android/app/Activity.html</a>

<strong>iOS</strong>: The same thing happens with iOS but in a different way and very rarely it occurs yet I never had this problem coz device memory will be good whe compared to android devices.

load your webView in ViewDidLoad() and dont load it in ViewDidAppear() or ViewWillAppear(), coz the later two methods will be called frequently and the first one is called when the app is Loaded.

Note: Your Webview will not reload automatically by you code, its reloading because any one of the above reasons.

Hope this documentation helps.


I am using web view in one of my app and i am not facing this problem. i am posting the code may be it could help you. if not tell me i have another solution in my mind.

import android.app.Activity; import android.app.ProgressDialog; import android.content.SharedPreferences; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { private WebView webView; boolean isLoadingComplete; Activity activity; private ProgressDialog progDailog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onPause() { super.onPause(); // Store values between instances here SharedPreferences preferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); // Put the values from the UI editor.putBoolean("isloaded", isLoadingComplete); // value to store // Commit to storage editor.commit(); } @Override protected void onResume() { super.onResume(); isLoadingComplete = false; activity = this; progDailog = ProgressDialog.show(activity, "Loading", "Please wait...", true); progDailog.setCancelable(false); webView = (WebView) findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { progDailog.show(); view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, final String url) { progDailog.dismiss(); isLoadingComplete = true; } }); SharedPreferences preferences = getPreferences(MODE_PRIVATE); isLoadingComplete = preferences.getBoolean("isloaded", false); if (!isLoadingComplete) { webView.loadUrl("http://www.statimllc.com/"); } else { progDailog.dismiss(); isLoadingComplete = true; } //webView.loadUrl("file:///android_asset/index.html"); } } }