一般我们使用webview的时候是直接在xml里面申明的,这样其实会造成一个问题当页面销毁了,webview是不会销毁的。为了解决这个问题 我推荐一种解决方案
首先xml中
<LinearLayout
android:id="@+id/linerWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
java代码
private LinearLayoutlinerWebView;
linerWebView = findViewById(R.id.linerWebView);
WebView webview =new WebView(AppApplication.getInstance().getApplicationContext());//这里使用application里面的上下文参数
webview.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
linerWebView.addView(webview);
@Override
protected void onDestroy() {
super.onDestroy();
linerWebView.removeView(webview);
webview.removeAllViews();
}
这样就能解决webview释放的问题