如何让webviewreturns到上一页不显示about:空白?

How to make to webview returns to the previous page and does not show about: blank?

P.S我的英文很烂,文字有误)))请见谅!

如何让webviewreturns到上一页不显示about:空白? 这是我的代码:

@Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return;
        }
        else {
            finish();
        }

        super.onBackPressed();
    }
 @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            mbErrorOccured = true;
            showErrorLayout();
            super.onReceivedError(view, errorCode, description, failingUrl);
            loadErrorPage();
        }
    }

    private void showErrorLayout() {
        mlLayoutRequestError.setVisibility(View.VISIBLE);
    }

    private void loadErrorPage() {
        if(mWebView!=null){
            String htmlData ="<html><body><div align= center >Check your internet!</div></body>" ;
            mWebView.loadUrl("about:blank");
            mWebView.loadDataWithBaseURL(null,htmlData, "text", "utf-8",null);
            mWebView.invalidate();
        }
    }

例如加载Google页面,然后发生错误加载about: blank,重新加载页面时,WebView重新加载about: blank而不是Google页面.如何使 Google 页面在重新加载时加载?

如果 about:blank 页面已经加载,然后您通过调用 webView.reload() 重新加载 webview,它只会重新加载当前页面,即 about:blank.

如果你想加载上一页,只需调用webView.goBack()或者你可以像这样直接加载url -> webView.loadUrl("<your.web.url>")

如果您想更好地控制 webview,请使用 WebViewClient and WebChromeClient

见下方代码

首先创建WebViewClient并赋值给webview。

var currentUrl : String = "https://www.google.com";
webview.webViewClient = MyWebViewClient()

在 WebViewClient 的 shouldOverrideUrlLoading 方法中,您可以使用自己的逻辑来决定要加载哪个网页。

 class MyWebViewClient : WebViewClient(){
        override fun onReceivedError(
            view: WebView?,
            request: WebResourceRequest?,
            error: WebResourceError?
        ) {
            super.onReceivedError(view, request, error)
            // here display your custom error message with retry feature
            displayErrorDialog()
        }
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {

            // update current url

            if (url != null) {
                currentUrl = url
            }

            return super.shouldOverrideUrlLoading(view, url)
        }
    }