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
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.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With