本文介绍了webView.canGoBack() 不再工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近我所有基于 WebView
的应用程序都开始出现后退按钮问题.某些 Android 组件的更新似乎存在问题,请帮助解决此问题或其他替代解决方案?
Recently all of my WebView
based apps started to have problem with back button. It looks an issue with update of some Android Component, Kindly help with the issue or some alternative solution?
webView.canGoBack()
总是返回 false.
webView.canGoBack()
always returning false.
PS:前几天所有应用都运行良好.
PS: All apps were working fine in previous days.
我是这样使用它的:
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
推荐答案
像这样创建自己的 canGoBack....
Create own canGoBack by like this....
ArrayList<String> urls = new ArrayList<>();
urls.add(www.google.com);
//after that you need override this method
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.d(TAG, "shouldOverrideUrlLoading: "+url);
urls.add(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
pDialog.show();
}
@Override
public void onPageFinished(WebView view, final String url) {
pDialog.dismiss();
}
});
//for onBackPressed, same like in custom button for back click option
@Override
public void onBackPressed() {
if(!canGoBack()
{
super.onBackPressed();
}
}
//Custome Method
public boolean canGoBack()
{
if(urls.size() == 1) // home page
{
return false;
}
else
{
webView.loadUrl(urls.get(urls.size()-2)); // load previous url
urls.remove(urls.size()-1); // remove last loaded url
return true;
}
}
这篇关于webView.canGoBack() 不再工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!