当应用程序退出后台并在iOS和Android中恢复运行时,WebView是否正在重新加载? [英] WebView is reloading when app is gone background and came back in iOS and Android?

查看:459
本文介绍了当应用程序退出后台并在iOS和Android中恢复运行时,WebView是否正在重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小应用程序,只不过拥有一个Webview.我正在使用网址加载Webview.但是当我的应用程序退出后台并返回时,Webview正在重新加载,对我来说,这应该不会发生.当应用程序在iOS和Android中进入后台时,如何停止重新加载Webview.

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.

注意: safari(iOS)和chrome(Android)都使用相同的url处理该事件,即使它们已经离开后台也是如此.他们没有重新加载Web视图或数据.

这是我的代码,我还需要添加其他内容吗?

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

如果每当Activity进入stop状态时将代码置于onCreate()方法中,并且设备内存不足,则进程将被终止,并将内存分配给高优先级应用程序.回来时,它会从oncreate()方法恢复,在这里,不仅是您的webView,而且整个应用程序都已重新加载.

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.

并且,如果您的activity处于stop状态,并且用户对其进行导航,则它将从onRestart()开始,在该位置它不会碰到onCreate,但会通过onStart(),在该位置您的应用程序不会重新启动,但是您放入onstartonResume的东西.

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.

基本上,您必须了解活动"状态,这是一个链接. https://developer.android.com/reference/android/app/Activity. html

Basically you have to understand the Activity states for this, Here is a link. https://developer.android.com/reference/android/app/Activity.html

iOS : iOS发生了相同的事情,但发生的方式不同,很少发生,但我从未遇到过这个问题,因为与Android设备相比,coz设备的内存会更好.

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.

将WebView加载到ViewDidLoad()中,不要将其加载到ViewDidAppear()ViewWillAppear()中,因为后两种方法将被频繁调用,第一种方法是在应用加载时调用的.

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.

注意:由于以上任何一种原因,您的代码不会自动重新加载Webview,而是重新加载.

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

希望此文档对您有所帮助.

Hope this documentation helps.

这篇关于当应用程序退出后台并在iOS和Android中恢复运行时,WebView是否正在重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆