17

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

luckysing_noobster
  • 1,933
  • 5
  • 24
  • 50
  • When user clicks close button in PIP mode, android calls activity's onStop() method. Same with restore button and onResume() – wingear Oct 24 '18 at 11:57

5 Answers5

16

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.

Artenes Nogueira
  • 1,422
  • 1
  • 14
  • 23
9

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);
}
Rahul Kamble
  • 202
  • 2
  • 3
2

@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)
}
Mariusz Wiazowski
  • 2,118
  • 1
  • 16
  • 17
1
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.

karrel
  • 19
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 26 '21 at 10:10
  • How does this even work..? – james04 Nov 09 '21 at 13:15
0

I needed to detect when the user clicked on the close button of the pip window. I ended up listening to the onDestroy function from the JitsiMeetActivity.

Alberto M
  • 1,608
  • 1
  • 18
  • 42