First Back button press not caught when playing a

2020-04-20 21:08发布

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

2条回答
做个烂人
2楼-- · 2020-04-20 21:40

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.

查看更多
贼婆χ
3楼-- · 2020-04-20 21:40

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

查看更多
登录 后发表回答