'Android back button and MediaController

I know how to take control of the back button. I have a VideoView embedded in a FrameLayout. My question is when the video pops up, the video controls are present for a few seconds. Hitting the back button while they are visible hides the video controls. Is there a way to ignore that function and do the next back action as if the video controls weren't visible?

The reason I ask is if I really do want to go back, I must hit the back button twice; once to hide the controls and second to actually go back



Solution 1:[1]

You can simply write:

mVideoView.setMediaController(new MediaController(this){
        public boolean dispatchKeyEvent(KeyEvent event)
        {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
                ((Activity) getContext()).finish();

            return super.dispatchKeyEvent(event);
        }
    });

No need to create new class.

Solution 2:[2]

The previous solutions no longer work with Android Pie +, you must instead :

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        mediaController.addOnUnhandledKeyEventListener((v, event) -> {
            //Handle BACK button
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
            {
                mediaController.hide(); //Hide mediaController,according to your needs, you can also called here onBackPressed() or finish() 
            }
            return true;
        });
    }

Solution 3:[3]

You can also have the Activity handle the event:

mVideoView.setMediaController(new MediaController(this){
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP) {
                ((Activity) getContext()).onBackPressed();
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }       
});

Then handle it in your Activity:

@Override
public void onBackPressed() {
    // clean up or send result here
    finish();
}

Solution 4:[4]

In Xamarin.Android, you can deal with this problem like this

public class CustomMediaController : MediaController
    {
        private FragmentActivity act;
        public CustomMediaController(Context context, FragmentActivity myActivity) : base(context)
        {
            act = myActivity;
        }
        public override bool DispatchKeyEvent(KeyEvent e)
        {
            if(e.KeyCode == Keycode.Back)
            {
                act.OnBackPressed();
            }
            return base.DispatchKeyEvent(e);
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serge Him
Solution 2 Didier116
Solution 3 Greg T
Solution 4 Arun Kumar