在 Android 中销毁 webview [英] Destroy webview in Android

查看:73
本文介绍了在 Android 中销毁 webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我尝试了很多示例来破坏 Android 中的 webview.

Firstly, a lot of example i had been tried for destroy the webview in Android.

例如:Android 内存泄漏

虽然我在 onDestroy() 中销毁了 webview 并以编程方式声明了一个 webview,但是我的 Android 设备也会出现内存泄漏问题.

Although i was destroying webview in the onDestroy() and declared a webview programmatically, but the memory leak problem also will be occurred in my Android device.

下面是我的代码..

public class MainActivity extends Activity {
private FrameLayout mWebContainer;
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);

    mWebContainer = (FrameLayout) findViewById(R.id.web_container);
    mWebView = new WebView(getApplicationContext());
    mWebContainer.addView(mWebView);
}

@Override
protected void onDestroy() {
    super.onDestroy();

    mWebContainer.removeAllViews();
    mWebView.clearHistory();
    mWebView.clearCache(true);
    mWebView.clearView();
    mWebView.destroy();
    mWebView = null;        
}

请有人帮助我..谢谢..

Someone help me please.. thank you..

推荐答案

WebView 可能不会被销毁,因为您正在删除 onDestroy() 中的视图,它可以在几种不同的情况下调用:当用户退出应用程序通过后退按钮,当用户按下主页按钮然后从最近的应用程序中滑动应用程序时,或者当系统杀死您的应用程序以为其他应用程序腾出空间时.在onDestroy()中销毁WebView可能会有问题.

The WebView might not be destroyed because you are removing the view in the onDestroy(), which can be called in a few different occasions: when the user exits the app via the back button, when the user presses the home button and then swipes the app from recents, or when the system kills your app to make room for other apps. It may be problematic to destroy the WebView in onDestroy().

旧答案:要从内存中删除 WebView,请覆盖finish() 方法并将onDestroy() 中的代码放入finish() 中.通过后退按钮退出应用程序时会调用完成,因此这将确保 WebView 被销毁.

Old Answer: To remove the WebView from memory, override the finish() method and place the code you have in onDestroy() in finish(). finish is called when the app is exited via the back button, so this will ensure that the WebView is destroyed.

新答案:我最初对 onDestroy 方法的调用时间是错误的,所以我和其他人编辑了问题以删除错误的部分.但是,这也稍微改变了我要销毁 WebView 的方法.可能是onDestroy中没有足够的时间销毁WebView,或者可能是Activity被多次被破坏,这会导致崩溃,因为 WebView 会被多次破坏并导致它崩溃(请参阅此答案底部的文档引用).解决方案是在销毁 WebView 时更加明确,并将 WebView 设置为 null,以便在尝试销毁它之前确保它没有被销毁.

New Answer: I was originally wrong about when the onDestroy method was called, so I and others have edited the question to remove the parts that were wrong. However, this also changes slightly what I would do to destroy the WebView. It may be that there is not enough time to destroy the WebView in onDestroy, or it could be that the Activity is getting destroyed multiple times, which would result in the crash, since the WebView would get destroyed multiple times and that would crash it (see the documentation quote at the bottom of this answer). The solution is to be more explicit when destroying the WebView and also to set the WebView to null so you can be sure that it wasn't destroyed before trying to destroy it.

当您使用 WebView.destroy() 时,WebView 在内部会自行销毁,但问题是无法确定您是否已在现有 WebView 对象上调用了 destroy.这是有问题的,因为在销毁后调用 WebView 上的方法会导致崩溃.解决方法是销毁WebView后设置为null.

When you use WebView.destroy(), internally the WebView will destroy itself, but the problem is that there is no way to determine if you've called destroy on an existing WebView object or not. This is problematic, because calling methods on the WebView after destroying it will result in a crash. The solution is to set the WebView to null after destroying it.

杀死视图的完整代码应如下所示(有些内容是可选的):

Your full code to kill the view should look like this (some things are optional):

public void destroyWebView() {

    // Make sure you remove the WebView from its parent view before doing anything.
    mWebContainer.removeAllViews();

    mWebView.clearHistory();

    // NOTE: clears RAM cache, if you pass true, it will also clear the disk cache.
    // Probably not a great idea to pass true if you have other WebViews still alive.
    mWebView.clearCache(true);

    // Loading a blank page is optional, but will ensure that the WebView isn't doing anything when you destroy it.
    mWebView.loadUrl("about:blank");

    mWebView.onPause();
    mWebView.removeAllViews();
    mWebView.destroyDrawingCache();

    // NOTE: This pauses JavaScript execution for ALL WebViews, 
    // do not use if you have other WebViews still alive. 
    // If you create another WebView after calling this, 
    // make sure to call mWebView.resumeTimers().
    mWebView.pauseTimers();

    // NOTE: This can occasionally cause a segfault below API 17 (4.2)
    mWebView.destroy();

    // Null out the reference so that you don't end up re-using it.
    mWebView = null;
}

这个方法应该在某个地方被调用,最好是在 finish() 中,因为它会被用户显式调用,但它也应该在 onDestroy() 中工作.

This method should then be called somewhere, preferably in finish() since that will be explicitly called by the user, but it should work in onDestroy() as well.

注意:我应该补充一点,调用 mWebView.onDestroy() 两次可能会导致浏览器崩溃.文档状态:

NOTE: I should add that calling mWebView.onDestroy() twice may crash the browser. The docs state:

销毁后不能在此 WebView 上调用其他方法.

No other methods may be called on this WebView after destroy.

这篇关于在 Android 中销毁 webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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