First Back button press not caught when playing a video android

蹲街弑〆低调 提交于 2020-01-11 12:06:09

问题


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();
        }
    }

回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!