Picture-in-Picture Mode Implementation

Picture-in-Picture (PiP): Android’s New Feature

Android 8.0 (API level 26) and later versions have introduced the Picture-In-Picture (PIP) mode. Smartphones running on Oreo can now use the PIP mode, which allows apps to be in PIP state so the user can view multiple windows at a time.

While an activity is in PIP mode it changes its configuration into a small window usually placed on one of the corners of the device’s screen. This view is also draggable and can be placed anywhere on the screen. PIP leverages the Multi-Window feature from Android 7.0 which is mostly used for showing a Video Stream on screen, while the user switches to other application or another screen within the same application.

A perfect example is a window showing Google Maps navigation in PIP mode while the user switches to another application.

How do you use PIP mode on Android?

To use PIP mode, you need to declare in your Manifest file that an Activity is designated to enter PIP mode.

Four important declarations are needed:

Switching to PIP mode

Launching Picture-in-Picture compatible app To take your app into picture in picture mode you need to call enterPictureInPictureMode() on your activity.

Either we can make an app enter the PIP mode on specific action or default user interaction like pressing ‘Home’ button or ‘More Tasks’ button. Using this action for triggering PIP mode is considered good practice, as it will allow the user to view video uninterruptedly without worrying about explicitly asking the app to switch to PIP mode. To achieve this, we can override method onUserLeaveHint()

 

When an app switches into or runs on PIP mode and we want to perform a set of operations like removing a UI or redrawing, we should do callbacks provided by overriding OnPictureInPictureModeChanged()

Heads Up

PIP mode is available on Android 8.0 and above versions if your device has sufficient juice (RAM). So before enabling an app to switch to PIP mode, it’s good practice to check if it is available or not by calling hasSystemFeature (PackageManager.FEATURE_PICTURE_IN_PICTURE) method. When you invoke enterPictureInPictureMode() the activity is set to paused state. Be careful not to let it affect streaming video!

Vivek Sharma

Vivek Sharma June 24, 2019 Tech Lead

0 Comment(s)

Contact Us