Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to catch android back button event

I am trying to catch the back button event for Android. I know there is a lot about this already on the forms, however, my code does not work as the examples given. Here is my code snippet to capture the event:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if(keyCode == KeyEvent.KEYCODE_BACK){
        Log.d(TAG, "back key captured");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

I also tried this:

@Override
public void onBackPressed(){
    Log.d(TAG, "in onBackPressed");
    finish();
}

The output from LogCat that either event got fired doesn't show up. Anyone know a possible reason for this?

Thanks.

like image 721
coder Avatar asked Nov 16 '11 21:11

coder


1 Answers

Another method to is to override the public void onBackPressed() method. It's more straightforward and easier to do.

like image 159
Brian Avatar answered Oct 05 '22 11:10

Brian