I have a weird error that I cant figure out how to fix . I have a video player - a simple videoview that plays a video. Now I ve implemented logic so that when the user presses the back button - it tells him to press the back button again to exit, and when he does it exits. But the problem is the first back press is not caught! I dont know what is causing this. I Press back once nothing happens, I press again and it shows me the toast that says "Please press back again to exit" and on pressing again it exits. The first one is not caught no matter what I do . This is the code for OnBackPressed()
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
finish();
}
else {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please press the back button again to exit",
Toast.LENGTH_SHORT).show();
}
}
Are you using MediaController for video user interaction? I hade something similar and in the end it turned out that the MediaController was catching the first user input even so it was hidden.
Thanks to @Satya got the answer(Commonsware) at Android back button and MediaController . Basically override dispatchKeyEvent to catch the back button press. @Satya - post your comment as an answer so I can mark it correct, marking this correct until then
来源:https://stackoverflow.com/questions/12740432/first-back-button-press-not-caught-when-playing-a-video-android