2011-07-05 90 views
1
@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。使用此代碼,進入網絡視圖後,我無法退出網絡視圖,除非我非常快地按兩次後退按鈕。有沒有辦法只按一次後退出網頁視圖?Android無法退出帶有後退按鈕的webview

回答

2

在WebViewClient的onPageFinished方法中,您必須清除歷史記錄,然後1次單擊將工作!

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