How to Implement Picture in Picture in AndroidOctober 01, 2021 Publish By : EXPERT APP DEVS 3 min read Viewed By : 1007
Starting from Android 8.0, it lets user to launch activities in picture-in-picture mode(PIP mode). Use case includes watching a video in a small pop-up window floating in the screen while allowing a user to perform navigation between apps or browsing content on the main screen. By default, the location of pip windows in on the corner of the main screen, also it's draggable so the user can move/drag it to another location on the main screen.
Following are the examples and usage of PIP in Mobile Technology PIP-enabled apps let users switch a video into PIP mode and allow it to watch till the end.
How to put everything together?
For adding PIP mode in the apps, we need to make some changes in Android Manifest.
The following set of attributes are required:
Switching your activity to picture-in-picture
enterInPictureInPictureMode must be called by activity to enter in picture-in-picture mode.
Although there are two other methods:
- Activity.enterPictureInPictureMode(PictureInPictureArgs args)
The above methods are marked as deprecated from SDK 27, leading to be removed in the next update - so you should use instead.
- enterPictureInPictureMode(PictureInPictureParams params)
If you want to include logic that enables activity to go into PIP mode instead of going into the background, then you can catch this kind of case by overriding onUserLeaveHint():
Examples include Google maps that switch to PIP mode whenever a user moves the application to the background while the app is still navigating.
Handling UI during picture-in-picture
System call of callsActivity.onPictureInPictureModeChanged() or Fragment.onPictureInPictureModeChanged(). , is carried out whenever activity enters or exits picture-in-picture mode.
To redraw the activity's UI state, you should override these callbacks. When activity is in PIP mode, meaning that the user can’t interact with UI elements, and details of small UI elements may be difficult to see. Also, during this activity is in small window form. Activity should only show video playback controls, by removing other UI elements before your activity enter PIP mode and restore the state when your activity becomes full-screen again:
Continuing video playback while in picture-in-picture
Activity’s onPause will be called when your activity switches to PIP mode by placing activity in paused state. For better user experience, the video playback should not be placed and must continue playing even if an activity is paused while in PIP mode.
What to do if another activity is already in PIP mode?
Sending another application to PIP mode, while there is an application that is already in PIP mode which leads to only one application to stay in PIP mode, priority will be for the activity that enters PIP mode first.
Following are the best practices to keep in mind when implementing PIP in your app.
- Before launching activity in PIP mode, a check must be done of devices having the necessary amount of RAM for best usage. hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE).
- Avoid showing anything on the pip window except for the video content for better user experience and also keep track of activity state for managing UI elements.
- Information that is critical must not be shown while an application is in PIP mode due to the small floating window size which leads to obscuring the area of the PIP window.
Need a consultation?
Drop us a line! We are here to answer your questions 24/7.