android-lifecycleonpausebackground-modeandroid-picture-in-picture

How to start picture in picture mode instead of going to background? Like youtube premium


I know how to enable picture-in-picture mode itself when an app runs normally but the problem is that I want to activate it when the app goes to background mode i.e. on activity pause. A good example of such behavior is YouTube premium app on android. When a video is playing and a user taps "home" button the video just keeps on playing in PIP mode. I tried to implement LifecycleObserver and launch picture-in-picture mode from its "onPause" callback. But it says "activity must be resumed to enter picture-in-picture". I took a look inside onPause method and it's implemented in a way that super.onPause() must be called first, so I can't just override it and not to call "super".

Does anyone have any suggestion on how implement this Youtube-like behavior? p.s. I've googled a lot before asking this


Solution

  • The docs provide an example for this:

    @Override
    public void onUserLeaveHint () {
        if (iWantToBeInPipModeNow()) {
            enterPictureInPictureMode();
        }
    }
    

    https://developer.android.com/guide/topics/ui/picture-in-picture#pip_button