我可以使用一些帮助来识别我的代码中有关片段和网络视图的问题。我尝试在其他线程中实现一些解决方案失败。我已经测试了这个相同的片段被替换而没有在内部创建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();
}
}
}
答案 0 :(得分: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();
}