Android无法使用后退按钮退出webview

时间:2011-07-05 09:07:32

标签: android

 @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);
 }

这段代码取自http://developer.android.com/guide/webapps/webview.html。使用此代码,在我进入Web视图后,除非我快速按两次后退按钮,否则我无法退出Web视图。有没有办法只按一下后退并退出网页视图?

1 个答案:

答案 0 :(得分:2)

在WebViewClient的onPageFinished方法中,您必须清除历史记录,然后单击一下就可以了!

    @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();
        }
    }