Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

You need to make the onKeyDownPress return true if the back button has been pressed. Here is how you should do that. If you don't return true, the touch event won't be passed on:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.e(LOG_TAG, "Key down = " + keyCode);
    if(keyCode == KeyEvent.KEYCODE_BACK){
        return true;
    }else{
    return super.onKeyDown(keyCode, event);
    }
}
backButtonRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top