[ACCEPTED]-onKeyDown not always called in Android app-events

Accepted answer
Score: 36

Is this onKeyDown in a view or in the activity?

If 11 setContentView is called passing in a view, and 10 that view has setFocusable(true) called 9 on it, all key events will bypass the activity 8 and go straight into the view.

On the other 7 hand, if your onKeyDown is in the view, and 6 you haven't called setContentView on the 5 Activity and setFocusable(true) on the view, then 4 your Activity will get the key events and 3 not the View.

Look for those specific calls 2 but I think you're right about it being 1 a focus issue.

Score: 18

Activity's onKeyDown/onKeyUp methods not 3 always called. Unlike them dispatchKeyEvent 2 on Activity fired always. Move keydown/keyup 1 logic here. Works well.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        // keydown logic
        return true;
    }
    return false;
}
Score: 0

insert this code

getWindow().getDecorView().setFocusable(true);
        if (SDK_INT >= Build.VERSION_CODES.O) {
            getWindow().getDecorView().setFocusedByDefault(true);
        }

0

More Related questions