一般我們使用webview的時(shí)候是直接在xml里面申明的恬涧,這樣其實(shí)會(huì)造成一個(gè)問題當(dāng)頁面銷毀了注益,webview是不會(huì)銷毀的。為了解決這個(gè)問題 我推薦一種解決方案
首先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里面的上下文參數(shù)
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釋放的問題