Maurice
Maurice

Reputation: 6433

Android cannot exit webview with back button

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
     // Check if the key event was the BACK key and if there's history
     if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() {
         myWebView.goBack();
         return true;
     }
     // If it wasn't the BACK key or there's no web page history, bubble up to the default
     // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
 }

This piece of code was taken from http://developer.android.com/guide/webapps/webview.html. With this code, after I enter the web view, I cannot exit of the web view unless I press the back button twice really fast. Is there a way to just press the back once and exit the web view?

Upvotes: 1

Views: 2333

Answers (1)

Maurice
Maurice

Reputation: 6433

In the onPageFinished method of the WebViewClient, you must clear the history, then 1 click will work!

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        if (url.equals("http://www.yourcurrenturl.com")) {              
            view.clearHistory();
        }
    }

Upvotes: 2

Related Questions