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
-
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 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.

- 1,422
- 1
- 14
- 23
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);
}

- 202
- 2
- 3
@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)
}

- 2,118
- 1
- 16
- 17
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.

- 19
- 1
-
1As 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
-
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.

- 1,608
- 1
- 18
- 42