I am developing an Android app. I have written my own code for back key event.
For that i am catching events using this method
public boolean onKeyDown(int keyCode, KeyEvent event) …..
I also want to use Menu options I have used Inflator for that.
But when I click on menu button, that event caught by my onKeyDown method.
I am not expecting that, I expect to execute following methods,
public boolean onCreateOptionsMenu(Menu menu){ // creating menu using MenuInflator }
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case :
return true;
default:
}
}
How could I use both the things on the same activity?? Any Idea ???
According to the documentation of
onKeyDown():it is normal that it receives the ‘menu button pressed’ event, as any
KeyEventbut if you return
false, this event will continue to be propagated and will therefore leadonOptionsItemSelected()to be calledSo you should code your
onKeyDown()in a way that lets theKeyEventto propagate if you do not handle it on your own.In the following example, we have two keys that are interesting to us: one we fully handle on our own, one we just track, and we ignore all the rest: