Wednesday, 16 October 2013

Handler the back button on Android

There are two methods to handle the back button on Android, depending on the API level.

For API level 5 or lower:

@Override
public void onBackPressed() {
    // Your code here
}

For API level higher than 5:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // your code
        return true; // To finish here and say the key has been handled
    }
    return super.onKeyDown(keyCode, event);
}

Source code:

No comments:

Post a Comment