Aug 18, 2011

Handle the Back button in WebView, to back in history.

Normally, we will have a Back button on browser, for user to go back in history. The default behaviour of the BACK button on Android system is Exiting the app. In order to change it's behaviour, we can override the onKeyDown() method: if it's BACK button pressed AND there has a back history item, than go back; otherwise pass to super method to handle as normal.

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK){
if(webView.canGoBack()){
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}





Related Post:

- Display Progress Bar on WebView when loading




next:

- Load local HTML from /assets



related:
- Create your own London 2012 App



No comments:

Post a Comment

Infolinks In Text Ads