Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect close and maximize clicked event in Picture In Picture mode in Android

How can I detect if the user clicked on the native close and maximize button in PIP small window . Are there any listeners I can listen to. Right now my receiver only listens to the controls I defined in my layout but what about the non custom buttons like the [] max button and the X close button which are part of PIP .See the link link

like image 283
luckysing_noobster Avatar asked Nov 02 '17 02:11

luckysing_noobster


3 Answers

It is not possible to detect clicks on any of the default PiP buttons.

When you activity enters in PiP mode, actually another system activity, called PiPMenuActivity, is started. Inside of it, it is set some OnClickListeners in these PiP buttons. When they are clicked, no broadcast, intent or something of the sorts is dispatched to the system so you could listen to it, neither the PiP API provides a method to attach a listener to these buttons.

The only way for now to detect that is to use your activitie's onResume and onStop methods. When the activity is restored from PiP, onResume and the onPictureInPictureModeChanged callbacks are called on your Activity. When the close button is clicked, the onStop and onPictureInPictureModeChanged callbacks are called.

like image 62
Artenes Nogueira Avatar answered Oct 12 '22 19:10

Artenes Nogueira


override fun onPictureInPictureModeChanged(
    isInPictureInPictureMode: Boolean,
    newConfig: Configuration?
) {
    if (isInPictureInPictureMode) {

    } else {
       if (lifecycle.currentState == Lifecycle.State.STARTED) {
           // todo finish your app
       }
    }
}

no other way I looked for it and I can solve it this way.

like image 34
karrel Avatar answered Oct 12 '22 19:10

karrel


Here is updated solution, working for me for close and maximize event.

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
    if(newConfig !=null){
        videoPosition = playerManager.getCurrentPosition();
        isInPipMode = !isInPictureInPictureMode;
    }
    if (getLifecycle().getCurrentState() == Lifecycle.State.CREATED) {
        finishAndRemoveTask();
        //when user click on Close button of PIP this will trigger, do what you want here
    }
    else if (getLifecycle().getCurrentState() == Lifecycle.State.STARTED){
        //when PIP maximize this will trigger
    }
    super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
}
like image 6
Rahul Kamble Avatar answered Oct 12 '22 21:10

Rahul Kamble