【问题标题】:Android WebView canGoBack always returns falseAndroid WebView canGoBack 总是返回 false
【发布时间】:2018-05-29 13:47:51
【问题描述】:

重现问题的步骤:

  1. 创建一个内部带有 WebView 的片段。

  2. 浏览 WebView 深处的几个站点,构建历史记录。

  3. 尝试使用 webview.canGoBack() 函数。

  4. 观察它总是返回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


【解决方案1】:

这里link

谈谈问题和可能的解决方案

【讨论】:

  • 此链接中的唯一解决方案是等到下一个 Chrome 版本 (65)。
【解决方案2】:

在最新的 Chrome 版本之后,WebView canGoBack 功能再次起作用:

64.0.3282.137

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多