2

我可以使用一些帮助来确定我的代码中有关片段和 web 视图的问题。我尝试在其他线程中实施一些解决方案未成功。我已经测试了相同的片段被替换而没有在内部创建 webview 并且没有泄漏。有任何想法吗?如果没有,任何人都可以提出替代解决方案吗?

这是我的 webview 片段:

public class CustomWebViewFragment extends PageFragment
{

private LinearLayout mWebContainer;
private WebView mWebView;


/**
 * public View onCreateView(LayoutInflater inflater, ViewGroup container,
 * Bundle savedInstanceState)
 */
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_one, container, false);

    //If I comment this line out, there is no memory leak
    mWebView = new WebView(this.getActivity().getApplicationContext()); 

    return v;
}


/**
 * public void onDestroy()
 */
@Override
public void onDestroy()
{
    super.onDestroy();
    if (mWebView != null)
    {
        mWebView.loadUrl("about:blank");
        mWebView.destroy();
        mWebView = null;
    }
}

}

这是我更改片段的方式:

@Override
public void onNavSelected(String page)
{
    if (page != null && !page.equals(""))
    {
        System.gc();
        if (page.equalsIgnoreCase(GlobalConstants.PAGE_1))
        {
            mCurrent = getFragment(); // Creates a new fragment
            getSupportFragmentManager().beginTransaction()
                .replace(R.id.main_fragment, mCurrent).commit();
        }
    }
}
4

1 回答 1

3

改变

//If I comment this line out, there is no memory leak
mWebView = new WebView(this.getActivity().getApplicationContext()); 

&

@Override
public void onDestroy()
{
    super.onDestroy();
    if (mWebView != null)
    {
        mWebView.loadUrl("about:blank");
        mWebView.destroy();
        mWebView = null;
    }
}

mWebView = new WebView(getActivity()); 

&

@Override
public void onDestroy()
{
    // null out before the super call
    if (mWebView != null)
    {
        mWebView.loadUrl("about:blank");
        mWebView = null;
    }
    super.onDestroy();
}
于 2013-10-10T18:58:21.090 回答