如何使 Webview 返回到上一页并且不显示 about:blank?

huangapple go评论85阅读模式
英文:

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

问题

@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></html>";
        mWebView.loadUrl("about:blank");
        mWebView.loadDataWithBaseURL(null, htmlData, "text", "utf-8", null);
        mWebView.invalidate();
    }
}

例如,加载了Google页面,然后出现错误,加载了about:blank,当重新加载页面时,WebView会重新加载about:blank,而不是Google页面。如何在重新加载时使Google页面加载?

英文:

P.S my English very bad and I have mistakes in text))) please sorry!

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

@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 =&quot;&lt;html&gt;&lt;body&gt;&lt;div align= center &gt;Check your internet!&lt;/div&gt;&lt;/body&gt;&quot; ;
            mWebView.loadUrl(&quot;about:blank&quot;);
            mWebView.loadDataWithBaseURL(null,htmlData, &quot;text&quot;, &quot;utf-8&quot;,null);
            mWebView.invalidate();
        }
    }

For example, the Google page loaded, then an error occurred and about: blank was loaded, and when the page is reloaded, the WebView reloads about: blank and not the Google page. How to make the Google page load on reload?

答案1

得分: 0

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

如果您想要加载先前的页面,只需调用webView.goBack(),或者您可以直接加载网址,如下所示-> webView.loadUrl("<your.web.url>")

如果您希望更好地控制webview,可以使用WebViewClientWebChromeClient

请参阅下面的代码示例:

首先创建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)
        // 在这里显示带有重试功能的自定义错误消息
        displayErrorDialog()
    }
    
    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        // 更新当前网址
        if (url != null) {
            currentUrl = url
        }
        return super.shouldOverrideUrlLoading(view, url)
    }
}
英文:

If about:blank page is already loaded and then you are reloading webview by calling webView.reload(), it will just reload the current page i.e. about:blank.

If you want to load previous page, just call webView.goBack() or you can directly load the url like this -> webView.loadUrl(&quot;&lt;your.web.url&gt;&quot;)

Use WebViewClient and WebChromeClient if you want to have a better control on webview.

See the code below

First create WebViewClient and assign it to webview.

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

In WebViewClient's shouldOverrideUrlLoading method, you can have you own logic to decide which web page you want's to load.

 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)
        }
    }

huangapple
  • 本文由 发表于 2020年10月6日 03:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定