第一次用bridgeWebview出现了一个问题,使用了webview布局的activity页面反复快速进入退出后页面白屏,app里所有使用webview的页面全部白屏,白屏后进入页面网页进度条会卡在20%,必须退出app重新进入才可以
在真机调试并不会报错,一直很难找到问题在哪。用模拟器调试才看到报错信息
报错信息,大体意思就是webview在destory的时候出现异常
报如下错误
Activityhas leaked window android.widget.ZoomButtonsController that wasoriginally added hereandroid.view.WindowLeaked:
是webview的ZoomButton报错,也就是那两个放大和缩小的按钮,导致的。如果设置为让他们出现,并且可以自动隐藏,那么,由于他们的自动隐藏是一个渐变的过程,所以在逐渐消失的过程中如果webview进行destory操作了,就会导致Leaked。
所以解决方案是,在destroy之前,先让他俩立马消失。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layerType="hardware">
<include
android:visibility="gone"
layout="@layout/actionbar_common_left_title"/>
<!--<com.github.lzyzsd.jsbridge.BridgeWebView-->
<!--android:id="@+id/webview"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"/>-->
<FrameLayout
android:id="@+id/fl_web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
把原来的webview换成FrameLayout,然后代码改成这样
//去掉布局中的webview,改为FrameLayout,在代码中实例化webview,防止webview内存泄漏
webview = new BridgeWebView(getApplicationContext());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
flWeb.addView(webview, layoutParams);
activity在ondestory的试试清空webview数据防止内存泄漏
@Override
public void onDestroy() {
if (webview != null) {
data = "";
params.clear();
flWeb.removeAllViews();
webview.removeAllViews();
webview.setWebChromeClient(null);
webview.setWebViewClient(null);
isFirst = true;
WebStorage.getInstance().deleteAllData(); //清空WebView的localStorage
webview.destroy();
}
super.onDestroy();
}
===============================================================================
2019.12.12
白屏问题记录:
h5页面有个返回按钮,点击的时候h5传递给android一个事件,android执行关闭activity操作,可是在关闭的同时h5执行了一段js,此时有个alert弹出,就会出现异常
再进入页面就一直白屏。
解决方法:h5端传递完事件不再执行逻辑操作