解决Android WebView内存释放

一般我们使用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释放的问题

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。