'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



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

@Artenes Nogueira you are right, is not possibile to detect click events on the default PiP buttons, but there is a way to know what's going on. You should override the onPictureInPictureModeChanged method and check the lifecycle of the activity.

Here you can find a self-explanatory code sample:

override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration?) {
    if (lifecycle.currentState == Lifecycle.State.CREATED) {
        //user clicked on close button of PiP window
        finishAndRemoveTask()
    }
    else if (lifecycle.currentState == Lifecycle.State.STARTED){
        if (isInPictureInPictureMode) {
            // user clicked on minimize button
        } else {
            // user clicked on maximize button of PiP window
        }
    }
    super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
}

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 Artenes Nogueira
Solution 2 karrel
Solution 3 Rahul Kamble
Solution 4 Mariusz Wiazowski