Search code examples
androidactionbarsherlockkey-events

Activity's onBackPressed method is never invoked


I am attempting to intercept back button events in my app for some custom functionality, but nothing I've written in the relevant callbacks is being executed, and I can't for the life of me understand why.

Here are my overridden methods:

@Override
public void onBackPressed() {
    super.onBackPressed();

    Log.e(LOG_TAG, "Back pressed");

    if (isMainScreenShowing) {
        finish();
    } else if (isTopLevelScreenShowing){
        loadNewScreen(new AccountBalanceInfoFragment());
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.e(LOG_TAG, "Key down = " + keyCode);
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    Log.e(LOG_TAG, "Key up = " + keyCode);
    return super.onKeyDown(keyCode, event);
}

If it matters, I am putting this in an activity that extends the SherlockFragmentActivity in ActionBarSherlock. The back button still works for popping the previous fragment transaction from the stack, but I cannot implement my own custom functionality.

Update: I noticed the following logcat output when hitting the back button:

10-01 16:42:49.879: D/InputEventConsistencyVerifier(7597): KeyEvent: ACTION_UP but key was not down.` 
10-01 16:42:49.879: D/InputEventConsistencyVerifier(7597):   in com.android.internal.policy.impl.PhoneWindow$DecorView{40cd4198 V.E..... R.....I. 0,0-480,800}
10-01 16:42:49.879: D/InputEventConsistencyVerifier(7597):   0: sent at 68941888000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x8, repeatCount=0, eventTime=68941888, downTime=68941823, deviceId=0, source=0x101 }

So the key down event is not being processed properly for some reason... Still a mystery to me.

Update 2: I should also copy the response that I wrote to SBerg's answer here since it is relevant --

I noticed that the onKeyUp callback now seems to be working. I'm not sure what I am doing differently yet. Key down and onBackPressed are still not invoked, however, which is very strange, but key up seems to be a sufficient work around for now.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.i(LOG_TAG, "Back pressed");
        // do stuff here
    }
    return super.onKeyUp(keyCode, event);
}

This is a suitable work-around for now, but it is a bit messy, and it would still be nice to understand why the first onBackPressed and onKeyDown callbacks are not invoked.


Solution

  • I figured I should put this into an answer so that it's easier to find:

    I noticed that the onKeyUp callback now seems to be working. I'm not sure what I am doing differently yet. Key down and onBackPressed are still not invoked, however, which is very strange, but key up seems to be a sufficient work around for now.

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i(LOG_TAG, "Back pressed");
            // do stuff here
        }
        return super.onKeyUp(keyCode, event);
    }
    

    This is a suitable work-around for now, but it is a bit messy, and it would still be nice to understand why the first onBackPressed and onKeyDown callbacks are not invoked.