At first I made an Override of onBackPressed in the activity. I can catch the back button pressed event correctly with this method. Later, to detect the press down and release action of the volume button, I implemented the onKeyDown and onKeyUp method. Then I found no matter I returned true or false from the onKeyDown and onKeyUp, the onBackPressed method never got fired. Does anyone know how to make onBackPressed and onKeyDown,OnkeyUp all to be called consequently?Any help is much appreciated!
Asked
Active
Viewed 891 times
2
-
Look at [dispatchKeyEvent](http://stackoverflow.com/a/2875006/2298490) I believe you have forgotten to add the `default: return super.dispatchKeyEvent(event); ` – grill2010 Apr 27 '17 at 11:05
-
You are totally correct. dispatchKeyEvent fixes my issue. What I used to return in default was super.onKeyUp(keyCode, event) and onKeyDown(keyCode, event) which would not pass the event to onBackPressed method. – user1870797 Apr 27 '17 at 18:27
-
I think you could post your answer in the answer zone so I can tick it as my accepted answer. – user1870797 Apr 27 '17 at 18:29
1 Answers
3
As I already said in the comments it is because you have forgotten to add the default: return super.dispatchKeyEvent(event);
in the dispatchKeyEvent method.
For better clarification just look at this example of dispatchKeyEvent