Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView clearHistory() on Webview canGoback() functionality?

  1. I'm trying to use the history as a check to exit the application. The home button reloads the page to google.com, but still retains page history even after I call .clearHistory().

  2. Here is the code for the Menu handling / selections:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      // Handle item selection
      switch (item.getItemId()) {
        case R.id.Home:
          mWebview.loadUrl("http://google.com");
          mWebview.clearHistory();
          return true;
        case R.id.About:
          Toast.makeText(this, "TEXT", Toast.LENGTH_SHORT).show();
        return true;
        default:
          return super.onOptionsItemSelected(item);
      }
    }
    
  3. Here is the code for the Exit function:

    @Override
    public void onBackPressed() {
      if (mWebview.canGoBack()) {
        mWebview.goBack();
      }
      else {
        super.onBackPressed();
      }
    }
    
like image 677
gERmanmIKrowave Avatar asked Nov 22 '13 17:11

gERmanmIKrowave


1 Answers

According to this post you need to call clearHistory when the page finished loading:

It appears that the history clears everything before the current page so if your browser is at page "A", you clear history and navigate to page "B" your history will be "A" "B", not just "B", but if you clear history when "B" finishes loading you will have only "B"

So instead of calling clearHistory right after loadUrl, you should envoke it from onPageFinished or onProcessChanged.

like image 133
Lizozom Avatar answered Oct 19 '22 17:10

Lizozom