我正在尝试从本地存储中展示网页到Webview页面,当页面第一次加载时看起来很好,但是当我删除Webview并再次添加它时,它就会像图片上那样(对于4.4+,在之前的Android版本中只有黑屏):
每次使用应用程序上下文以编程方式添加webview,关闭缓存、cookie,并在on destroy中删除它。这是我的代码:
mLayout = (LinearLayout) view.findViewById(R.id.linear_main);
mWebView = new WebView(getActivity().getApplicationContext());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSaveFormData(false);
mWebView.setInitialScale(1);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setDatabaseEnabled(false);
mWebView.getSettings().setDomStorageEnabled(false);
mWebView.getSettings().setGeolocationEnabled(false);
mWebView.getSettings().setSaveFormData(false);
mWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
mWebView.setWebChromeClient(new WebChromeClient());
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
mLayout.addView(mWebView,0);
/// then display page by call
public void display(String path){
Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
noCacheHeaders.put("Pragma", "no-cache");
noCacheHeaders.put("Cache-Control", "no-cache");
mWebView.loadUrl(path, noCacheHeaders);
}
/// after display destroy fragment with the WebView
@Override
public void onDestroy() {
super.onDestroy();
CookieSyncManager.createInstance(getActivity().getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
mWebView.removeAllViews();
mWebView.destroyDrawingCache();
mWebView.clearDisappearingChildren();
mWebView.clearFormData();
mWebView.clearHistory();
mWebView.clearCache(true);
mWebView.loadUrl("about:blank");
mWebView.pauseTimers();
mLayout.removeAllViews();
mWebView.destroy();
mWebView = null;
getActivity().getApplicationContext().deleteDatabase("webview.db");
getActivity().getApplicationContext().deleteDatabase("webviewCache.db");
}
请问还有什么其他尝试的方法吗 =)