How do I clear previous WebView's content before loading the next WebView?

前端 未结 7 1270

My scenario is initially I am loading a WebView and later on I am loading the same WebView with a different URL.

My problem is whenever I am loading the next URL I c

相关标签:
7条回答
  • 2020-12-15 18:03

    use mweb.clearView(); before loading the new URL.

    Edit:

    clearView() was deprecated in API level 18. Use WebView.loadUrl("about:blank") to reliably reset the view state and release page resources (including any running JavaScript).

    to solve the issue raised by @Liang:

    To have a consistent back navigation, you can override "onPageFinished", and inside it check whether the url contains "about:blank" or not. if yes, use "webview.goBack()"

    0 讨论(0)
  • 2020-12-15 18:05

    It seems to set the layer type of webview as "software" can resolve the problem. Perhaps the previous contents are caused by drawing cache.

    0 讨论(0)
  • 2020-12-15 18:07

    Create a string with the HTML for a blank page and load that into the webview. Or hide it until it is loaded.

    0 讨论(0)
  • 2020-12-15 18:10

    use following statement before loadData [Kuldip Bairagi]

    view.loadUrl("javascript:document.open();document.close();");

    0 讨论(0)
  • 2020-12-15 18:13

    I guess you can use Webview.clearHistory(). But remember so impotent to call this after WebView is loaded. For example the code:

    webView.loadData(someUrl);
    webView.clearHistory();
    

    won't work because webView is not loaded. We have to clear it like:

    webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    clearHistory();
                }
    }
    
    0 讨论(0)
  • 2020-12-15 18:18

    I usually hide the webview before loading the new url, and then show it again when the loading progress has advanced a bit.

    // url loading
    mWebView.setVisibility(View.INVISIBLE);
    mWebView.loadUrl(newUrl);
    
    // show webview again
    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (progress >= 10) {
                mWebView.setVisibility(View.VISIBLE);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题