We have enterPictureInPictureMode()
to move an activity from its current form into a picture-in-picture representation.
What is the means by which we revert that, returning the activity to its normal state, besides destroying the activity? There is no exitPictureInPictureMode()
, leavePictureInPictureMode()
, or janeGetMeOffThisCrazyPictureInPictureModeThing()
method on Activity
, and the documentation does not seem to cover an alternative.
I am interested in a solution for Android O, for picture-in-picture mode on mobile devices, though if that works for Android TV too, wonderful!
UPDATE 2017-04-08: If what you want is to return to normal mode when the user clicks the X button to exit picture-in-picture mode, you can do something like this:
@Override public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { super.onPictureInPictureModeChanged(isInPictureInPictureMode); if (!isInPictureInPictureMode) { getApplication().startActivity(new Intent(this, getClass()) .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); } }
The key bits are to call startActivity()
to start the current activity again with FLAG_ACTIVITY_REORDER_TO_FRONT
. With a singleTask
activity, you need to call that on some non-Activity
context, such as the Application
singleton. This does not appear to trigger onStop()
or onStart()
, but it does trigger onNewIntent()
(with whatever Intent
you pass to startActivity()
).
Open Settings. Tap Apps & notifications. Go to Advanced > Special app access. Select Picture-in-picture.
Now, tap on Picture in Picture. Toggle off the switch next to Start PiP Automatically. This setting ensures that picture-in-picture mode is only enabled when you tap the PiP mode icon and not when you navigate to Home Screen. That’s all for now!
I don't think the activity can decide to leave Picture-in-picture mode. The activity entering PIP mode goes into the paused state, but remains started. If the user taps the PIP activity, the system shows a menu for the user to interact with; no touch events reach the activity while it is in the PIP state.
To do that, go to Settings > General > Picture-in-Picture and turn off the switch next to Start PiP Automatically. Note: Even with the setting above turned off, you can still open videos in Picture-in-Picture mode manually via the PiP icon or the two-fingered double-tap gesture.
How to Enable Picture in Picture Mode on iPhone 1 Play a Video on any supported app 2 Now tap on the PiP Mode icon or navigate to Home Screen, the video will instantly minimize 3 Hold & drag the minimized screen anywhere, according to your preference.
Move the activity to the back
activity.moveTaskToBack(false /* nonRoot */);
restore the activity to the front
Intent startIntent = new Intent(PipActivity.this, PipActivity.class); startIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); activity.startActivity(startIntent);
I had the same problem:
MainActivity
opens VideoPlayerActivity
which has PIP mode enabled.MainActivity
.X
) button on the PIP window.VideoPlayerActivity
. While none of the above solutions work for me, I found out that the only way to listen to X
button is to override onStop
.
When the activity is restored from PIP, onResume
and onPictureInPictureModeChanged
are called, when the X
button is clicked, the onStop
and onPictureInPictureModeChanged
are called.
So I tried to call finish()
inside onPictureInPictureModeChanged
when the onStop
is alreay called.
override fun onStop() { super.onStop() onStopCalled = true } override fun onResume() { super.onResume() onStopCalled = false } override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) { if (isInPictureInPictureMode) { // ... } else { if (onStopCalled) { finish() } } }
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