问题描述
Webview的历史记录尚未清除...下面的代码有什么问题?
The history of webview is not clearing... What is wrong with below code?
创建Web视图
mWebViewReport=(WebView)findViewById(R.id.report_page);
mWebViewReport.setWebViewClient(new HelloWebViewClient());
mWebViewReport.getSettings().setBuiltInZoomControls(true);
单击帮助按钮时加载帮助文件
Load help file when help button click
mWebViewReport.loadUrl("file:///android_asset/help.html");
mWebViewReport.clearHistory();
mWebViewReport.clearCache(true);
点击摘要按钮时
加载摘要文件
load Summary file when summary button click
mWebViewReport.loadUrl("file:///android_asset/summary.html");
//On back button click
if (mWebViewReport.canGoBack()) {
mWebViewReport.goBack();
return ;
}
在这里我也可以看到帮助"页面...
Here i can see the Help page too...
推荐答案
您无法在webview加载页面(url)时清除历史记录,以便按如下方式清除onPageFinished侦听器的历史记录设置
You can't clear history while the webview is loading a page (url) in order to clear the history setup onPageFinished listener as follows
在onCreate之前声明一个公共变量
declare a public var before the onCreate
boolean clearHistory = false;
现在,当您声明mWebViewReport时,请进行设置
now when you declare your mWebViewReport set this up
mWebViewReport.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url)
{
if (clearHistory)
{
clearHistory = false;
mWebViewReport.clearHistory();
}
super.onPageFinished(view, url);
}
});
现在,当您调用要清除历史记录的帮助URL时,只需将clearHistory设置为true
Now when you call your help url insted of clearing the history just set clearHistory to true
mWebViewReport.loadUrl("file:///android_asset/help.html");
mWebViewReport.clearHistory(); // REMOVE THIS LINE
mWebViewReport.clearCache(true); // REMOVE THIS LINE
clearHistory = true; // ADD THIS LINE
这篇关于如何清除网页视图的历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!