3amoura February 2016

close fullscreen of Youtube Player on back button inside fragment

I am using Master/Detail layout in a tablet, on the left few buttons to open several fragments, one of the fragments contains youtube player.

The Problem,

When the youtube player is full screen, and i press back button, The activity onBackPressed is called, and the whole activity is closed.

What i have tried,

1- Added on KeyListener for parent fragment (which contains the Youtube Fragment) and handle when click on back button, but this listener is called only if the player is not fullscreen, otherwise it is not called,

rootView.setFocusableInTouchMode(true);
        rootView.requestFocus();
        rootView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    try {
                        // Close full screen
                        return true;
                    } catch (Exception e) {
                        AHHExceptionHandler.handleException(e);
                    }
                }
                return false;
            }
        });

2- Added onKeyListener to youtubeFragment View to check if it is full screen then close the full screen mode

youTubeFragment.getView().setFocusableInTouchMode(true);
youTubeFragment.getView().requestFocus();
    youTubeFragment.getView().setOnKeyListener(new View.OnKeyListener() {
                                                @Override
                                                public boolean onKey(View v, int keyCode, KeyEvent event) {
                                                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                                                        try {
                                 

Answers


Andre Classen February 2016

You can overwrite onBackPressed() in your activity and in the case your player is fullscreen you can handle it here and call NOT super.onBackPressed().

If it's not fullscreen call super.onBackPressed().

Post Status

Asked in February 2016
Viewed 3,487 times
Voted 14
Answered 1 times

Search




Leave an answer