【发布时间】:2018-05-29 13:47:51
【问题描述】:
重现问题的步骤:
创建一个内部带有 WebView 的片段。
浏览 WebView 深处的几个站点,构建历史记录。
尝试使用 webview.canGoBack() 函数。
观察它总是返回false。
这是一种新行为,因为之前 WebView 确实返回了 true。
似乎新版本的 WebView 破坏了此功能。
这是我的代码:
class MyWebViewClient extends WebViewClient {
// Loads all hyperlinks in the existing WebView
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Protection against getActivity() returning null object reference
if (!isAdded()) return true;
// Allow WebView to load url
Uri uri = Uri.parse(url);
// Google Maps
if (url.contains("maps.google")) {
Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(mapIntent);
}
return true;
}
// Telephone
else if (url.contains("tel:")) {
String phoneNumber = url.substring(4);
Utils.dialPhone(getActivity(), phoneNumber);
return true;
}
// Redirect
return false;
}
}
这里是我使用 goBack API 的地方:
public boolean canWebViewGoBack() {
if (mWebView != null) {
if (mWebView.canGoBack()) {
return true;
}
}
return false;
}
public void webViewGoBack() {
if (mWebView != null) {
if (mWebView.canGoBack()) {
mWebView.goBack();
}
}
}
【问题讨论】:
-
或者是网站...
-
是的,你是对的,似乎新版本的 WebView 破坏了这个功能。这里也一样。
标签: java android google-chrome webview