35

The history of webview is not clearing... What is wrong with below code?

Web view Creation

mWebViewReport=(WebView)findViewById(R.id.report_page);
mWebViewReport.setWebViewClient(new HelloWebViewClient());
mWebViewReport.getSettings().setBuiltInZoomControls(true);

Load help file when help button click

mWebViewReport.loadUrl("file:///android_asset/help.html");
mWebViewReport.clearHistory();
mWebViewReport.clearCache(true);

load Summary file when summary button click

  mWebViewReport.loadUrl("file:///android_asset/summary.html");

    //On back button click
     if (mWebViewReport.canGoBack()) {
            mWebViewReport.goBack();
            return ;
      }

Here i can see the Help page too...

Lii
  • 11,553
  • 8
  • 64
  • 88
vnshetty
  • 20,051
  • 23
  • 64
  • 102

3 Answers3

70

You can't clear history while the webview is loading a page (url) in order to clear the history setup onPageFinished listener as follows

declare a public var before the onCreate

boolean clearHistory = false;

now when you declare your mWebViewReport set this up

mWebViewReport.setWebViewClient(new WebViewClient(){

    @Override
    public void onPageFinished(WebView view, String url) 
    {
        if (clearHistory)
        {
            clearHistory = false;        
            mWebViewReport.clearHistory();
        }
            super.onPageFinished(view, url);
    }
});

Now when you call your help url insted of clearing the history just set clearHistory to true

mWebViewReport.loadUrl("file:///android_asset/help.html");
mWebViewReport.clearHistory();  // REMOVE THIS LINE
mWebViewReport.clearCache(true); // REMOVE THIS LINE
clearHistory = true; // ADD THIS LINE
Louie Bertoncin
  • 3,744
  • 2
  • 25
  • 28
Osama_Almaani
  • 896
  • 7
  • 16
  • 1
    I didn't realize clearHistory() needed to be called in onPageFinished(). I was calling it, nothing was happening-- I am guessing because my webview was still loading content asynchronously. Anyway, thank you for this answer. – Richard Jun 07 '15 at 22:05
  • 1
    It's a good solution but it does have a few issues. If the user presses the back key before the new page finishes loading (it could be a lengthy load), clearHistory doesn't get called any more. I couldn't really find a good way around this, except to mess around with the back key treatment which would be very complicated to do thoroughly. (There can be workarounds to particular situations, I did find an acceptable one for mine, but not a general one.) Besides, I got some crashes that I strongly suspect are related to calling clearHistory. So beware. – VSim Nov 06 '17 at 02:39
  • @VSim hello, could you tell me that how to clear history with back key – zys Mar 21 '19 at 12:51
  • @zys Sorry, not really. If you read my comment, my experience has been that it's complicated to do well and you probably shouldn't do it. So my advice would be to think it over carefully, maybe you find another way to do what you actually want to accomplish. At least that's what I did. – VSim Mar 21 '19 at 18:35
13

I think, clearHistory() clears the back and forward list. So the currently loaded url will still remain in the back list.

abhinav
  • 3,199
  • 2
  • 21
  • 25
  • Not impossible, you'll just have to use it before `mWebViewReport.loadUrl("file:///android_asset/summary.html");` if you don't care about what is in your `back or forward` list, or use flags to know when to `clearhistory`. There can be numerous ways depending on your requirement and scenario. At the moment, it is probably not working because the help page is loaded when you use `clearhistory`. Hope this helps. – abhinav Nov 12 '11 at 09:41
  • 1
    I am not sure about how you want your navigation to work. But can you make use of this? `goBackOrForward(-2)` – abhinav Nov 12 '11 at 09:57
  • 1
    using webView.goBackOrForward(Integer.MIN_VALUE) before calling clearHistroy; seems to do the trick. – Aamir Abro Nov 11 '14 at 12:27
  • I can't thank you enough! I was struggling to realize this is what's happening for a while now. This is the only thing that makes sense: you have to clear the history after you've navigated away from the page you don't want in history. – TWiStErRob Aug 04 '23 at 23:14
7

The solution from Osama_Almaani works very fine! You can optimize your code by moving the flag into the WebViewClient implementation.

Define a field in your activity and use this instead of the anonymous declaration:

private MyWebViewClient myWebViewClient = new MyWebViewClient();
...
myWebView.setWebViewClient(myWebViewClient);

Enhance your WebViewClient class with a new method clearHistory():

private class MyWebViewClient extends WebViewClient {
  private boolean clearHistory = false;

  /**Use this instead of WebView.clearHistory(). */
  public void clearHistory(){
    clearHistory = true;
    }

  @Override
  public void onPageFinished(WebView view, String url) {
    if(clearHistory){
      Log.d(TAG, "clearHistory now");
      clearHistory = false;
      myWebView.clearHistory();
    }
    super.onPageFinished(view, url);
    }
}

Now you can use this new method:

myWebViewClient.clearHistory();
mWebViewReport.loadUrl("file:///android_asset/help.html");
Christian Schulzendorff
  • 1,431
  • 1
  • 18
  • 15