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.
Note: 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.
This is my code, do I need to add anything more:
iOS:
NSURL* url = [NSURL URLWithString:ip];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Android:
String url = "http://"+ip+":8081";
webView.loadUrl(url);
There are several reasons for the reloading of the App.
Android
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. https://developer.android.com/reference/android/app/Activity.html
iOS: 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With