销毁的WebView Android中 [英] Destroy webview in Android

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

问题描述

首先,有很多例子我已经试过了摧毁Android的web视图。

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

例如: 内存泄漏的Andr​​oid

虽然我被摧毁的WebView中的onDestroy()和编程宣布web视图,但内存泄漏问题也随之出现在我的Andr​​oid设备。

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..

推荐答案

web视图可能不会因为你是除去了的onDestroy(),它可以被称为在几个不同场合的看法破坏:当用户退出通过后退按钮,当用户presses主页按钮,然后应用程序挥笔该应用从最近通话,或当系统杀死你的应用程序,以腾出空间给其他应用程序。这可能是有问题的摧毁的onDestroy()的web视图。

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,覆盖完成()方法,并将code你在的onDestroy()在完成()。完成时调用的应用程序是通过返回按钮退出,所以这将确保的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 web视图,或这可能是因为该活动是被破坏掉了多次,这将导致崩溃,因为web视图会被摧毁多次,这会崩溃(见这个答案底部的文档引号)。解决的办法是销毁的WebView,并设置web视图为空,所以你可以肯定的是它试图摧毁它之前不被破坏时要更加明确。

New Answer: I was wrong about when the onDestroy method was called (I'll blame my naivety) 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(),内部的web视图会毁灭自己,但问题是,有没有办法确定,如果你已经呼吁摧毁一个web视图对象与否。这是因为它不为空,你不能调用该对象的任何更多的方法,而没有得到段错误。解决的办法是设置web视图为空,而在内部的行为方式相同的destroy(),并为您提供了能够标志的WebView是否已被破坏的好处。

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 a WebView object or not. This is because it is not null and you cannot call any more methods on that object without getting the segfault. The solution is to set the WebView to null, which internally acts the same way as destroy() and gives you the added benefit of being able to flag whether the WebView has been destroyed.

您充分code杀掉认为应该是这样的:

Your full code to kill the view should look like this:

public void destroyWebView() {

    mWebContainer.removeAllViews();

    if(mWebView != null) {
            mWebView.clearHistory();
            mWebView.clearCache(true);
            mWebView.loadUrl("about:blank");
            mWebView.freeMemory(); 
            mWebView.pauseTimers();
            mWebView = null;
    }

}

此方法应该再被地方叫,pferably $ P $的结束(),因为这将显式调用用户,但它应该在的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.destroy()和mWebView =空做同样的事情。另外,我想补充一点,叫 mWebView.onDestroy 两次或 mWebView = NULL 两次会导致浏览器崩溃,因为我说明 mWebView = NULL 是由web视图PTED为mWebView.destroy()除$ P $和毁坏后,它为空。所以基本上做了两次,导致本地空指针,这将表现为致命的信号11 。该文档状态:

NOTE: mWebView.destroy() and mWebView = null do the exact same thing. Also, I want to add that calling mWebView.onDestroy twice or mWebView = null twice will crash the browser since as I stated mWebView = null is interpreted by the WebView as mWebView.destroy() and after destroying it, it is null. So basically doing that twice results in a native NullPointer, which will manifest itself as Fatal Signal 11. The docs state:

没有其他方法可以称为这个的WebView破坏后。

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

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

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