Picture-in-picture Support

Android 8.0 (API level 26) allows activities to launch in picture-in-picture (PIP) mode. PIP is a
special type of multi-window mode mostly used for video playback.
It lets the user watch a video in a small window pinned to
a corner of the screen while navigating
between apps or browsing content on the main screen.

Alas, your browser doesn't support HTML5 video. That's OK! You can still
download the video and watch it with a video player.

PIP leverages the multi-window APIs available in Android 7.0 to
provide the pinned video overlay window. To add PIP to your app, you need to
register your activities that support PIP, switch your activity to PIP mode as
needed, and make sure UI elements are hidden and video playback continues when
the activity is in PIP mode.

The PIP window appears in the top-most layer of the screen, in a corner chosen by the system. You
can drag the PIP window to another location. When you tap on the window two special controls appear:
a full-screen toggle (in the center of the window) and a close button (an "X" in the upper right
corner).

Your app controls when the current activity enters PIP mode. Here are some examples:

An activity can enter PIP mode when the user taps the home or recents
button to choose another app. (This is how Google Maps continues to display directions while the user
runs another activity at the same time.)

Your app can move a video into PIP mode when the user navigates
back from the video to browse other content.

Your app can switch a video into PIP mode while a user watches the end
of an episode of content. The main screen displays promotional or summary
information about the next episode in the series.

Your app can provide a way for users to queue up additional content while
they watch a video. The video continues playing in PIP mode while the main
screen displays a content selection activity.

Declaring picture-in-picture support

By default, the system does not automatically support PIP for apps.
If you want support PIP in your app, register your video
activity in your manifest by setting
android:supportsPictureInPicture and
android:resizeableActivity to true. Also, specify
that your activity handles layout configuration changes so that your activity
doesn't relaunch when layout changes occur during PIP mode transitions.

Switching your activity to picture-in-picture

To enter picture-in-picture mode, an activity must call
enterPictureInPictureMode(). For example, the following
code switches an activity to PIP mode when the user clicks a dedicated button in the app's UI:

Java

You might want to include logic that switches an activty into PIP mode instead of going into the
background. For example, Google Maps switches to PIP mode if the user presses the home or recents
button while the app is navigating. You can catch this case by overriding onUserLeaveHint():

Handling UI during picture-in-picture

You should override these callbacks to redraw the activity's UI elements. Keep in mind that in
PIP mode your activity is shown in a small window. Users cannot interact with UI elements when in
PIP mode and the details of small UI elements may be difficult to see. Video playback activities
with minimal UI provide the best user experience. The activity should only show video playback
controls. Remove other UI elements before your activity enters PIP and restore them when your
activity becomes full-screen again:

Continuing video
playback while in picture-in-picture

When your activity switches to PIP, the system places the activity in the
paused state and calls the activity's onPause() method. Video playback should not be paused and should continue
playing if the activity is paused while in PIP mode.

In Android 7.0 and later, you should pause and resume video playback when the system
calls your activity's onStop() and
onStart(). By doing this, you can avoid
having to check if your app is in PIP mode in
onPause() and explicitly
continuing playback.

If you have to pause playback in your onPause() implementation, check for PIP mode by calling isInPictureInPictureMode() and handle playback appropriately, for example:

When your activity switches out of PIP mode back to full-screen mode, the
system resumes your activity and calls your
onResume() method.

Using a single playback activity for
picture-in-picture

In your app, a user might select a new video when browsing for content on
the main screen, while a video playback activity is in PIP mode. Play the new
video in the existing playback activity in full screen mode, instead of
launching a new activity that might confuse the user.

To ensure a single activity is used for video playback requests and
switched into or out of PIP mode as needed, set the activity's
android:launchMode to singleTask in your manifest:

When your app is in PIP mode, video playback in the PIP window can cause
audio interference with another app, such as a music player app or voice search
app. To avoid this, request audio focus when you start playing the video,
and handle audio focus change notifications, as described in
Managing Audio
Focus. If you receive notification of audio focus loss when in PIP mode,
pause or stop video playback.