28

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 can see the previously loaded URL contents and then my currently loaded URL contents gets displayed.

I want to clear the contents whenever I am loading the different URL.

Say,

if (pos == 0) {
    mweb.clearCache(true);
    mweb.clearHistory();
    mweb.loadUrl("http://stackoverflow.com/");
}
else if (pos == 1) {
    mweb.clearCache(true);
    mweb.clearHistory();
    mweb.loadUrl("http://android.stackexchange.com/");
}

Both clearCache() and clearHistory() does not work for me.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
Jack Dsilva
  • 1,504
  • 3
  • 24
  • 44

7 Answers7

51

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()"

a fair player
  • 11,530
  • 9
  • 46
  • 48
  • 6
    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). – a fair player Nov 06 '13 at 07:22
  • 12
    `WebView.loadUrl("about:blank")` will add a blank page in `BackForwardList`, so it will go back to the blank page if using `WebView.goBack()`. Can I remove the blank page in `BackForwardList`? – Liang Jun 10 '14 at 10:21
  • 1
    Anyone find a quick solution for the issue @Liang brought up? about:blank works except it adds a blank page in the back history. – loeschg Dec 03 '14 at 22:04
  • you can override "onPageFinished" and check whether the url contains "about:blank" or not. if yes, use "webview.goBack()" – a fair player Dec 05 '14 at 11:12
  • 1
    Hi @afairplayer, I try to `override onPageFinished" and check whether the url contains "about:blank" or not. if yes, use "webview.goBack()` but it doesn't work. Most probably I misunderstand that statement. Do you have a minimal code to demonstrate the idea? – Cheok Yan Cheng Aug 05 '15 at 02:25
  • @CheokYanCheng i'm using webView.clearView(). I know, it's deprecated, but this only acceptable solution for me now – Dehimb Mar 15 '16 at 17:42
  • This worked for me. I added WebView.loadUrl("about:blank") in onDestroy() method and set same line in onBackPressed() & it worked like a charm. – Däñish Shärmà Apr 23 '18 at 17:46
5

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);
        }
    }
}
Pimentoso
  • 673
  • 8
  • 14
5

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();
            }
}
Djek-Grif
  • 1,391
  • 18
  • 18
1

Thnx to StefanK comment found Here, You need to use the clearHistory() method right when the page finishes loading in the WebViewClient onPageFinished(WebView view, String url) method also in the onLoadResource(WebView view, String url) method; as follows:

    myCoolWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onLoadResource(WebView view, String url) {
            // Notice Here.
            view.clearHistory();
            super.onLoadResource(view, url);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // And Here.
            view.clearHistory();
            super.onPageFinished(view,url);
        }
    });

Also you can check the canGoBack() and copyBackForwardList() for manipulations.

E_X
  • 3,722
  • 1
  • 14
  • 15
0

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

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

ThirdPrize
  • 194
  • 1
  • 13
-5

use following statement before loadData [Kuldip Bairagi]

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