'java.lang.IllegalStateException: enterPictureInPictureMode: Current activity does not support picture-in-picture

Error message

java.lang.IllegalStateException: enterPictureInPictureMode: Current activity does not support picture-in-picture.
        at android.os.Parcel.createException(Parcel.java:2079)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.os.Parcel.readException(Parcel.java:1987)
        at android.app.IActivityTaskManager$Stub$Proxy.enterPictureInPictureMode(IActivityTaskManager.java:6824)
        at android.app.Activity.enterPictureInPictureMode(Activity.java:2728)
        at com.MapActivity.enterPIP(apActivity.java:2892)
        at com.MapActivity.onUserLeaveHint(MapActivity.java:2915)
        at android.app.Activity.performUserLeaving(Activity.java:8015)
        at android.app.Instrumentation.callActivityOnUserLeaving(Instrumentation.java:1517)
        at android.app.ActivityThread.performUserLeavingActivity(ActivityThread.java:4417)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4402)
        at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.wm.ActivityTaskManagerService.ensureValidPictureInPictureActivityParamsLocked(ActivityTaskManagerService.java:4263)
        at com.android.server.wm.ActivityTaskManagerService.enterPictureInPictureMode(ActivityTaskManagerService.java:4140)
        at android.app.IActivityTaskManager$Stub.onTransact(IActivityTaskManager.java:3262)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

My setting value and code.

AndroidManifest.xml

...
android:name="com.test.MapActivity"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:configChanges="orientation|smallestScreenSize|screenLayout|screenSize"
android:exported="true"
...

MapActivity.java

public void enterPIP(){
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ...
        PictureInPictureParams params = new PictureInPictureParams.Builder()
                .setAspectRatio(new Rational(width, height)).build();

        ...
        boolean supportsPIP =
                packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
        if (supportsPIP) {
            Log.d("PIP", "*** Picture-in-Picture ***");
            enterPictureInPictureMode(params);
        } else {
            Log.d("PIP", "*** No support Picture-in-Picture ***");
        }
    }
}

APP is crashing after calling enterPictureInPictureMode(params);

It works in an activity with simple buttons, but not work in the MapActivity, which is composed with complicated elements.



Solution 1:[1]

Finally, I found out the problem! Another activity extends this MapActivity. I declared PIP configuration for this activity in the AndroidManifest, it can work. :D

Solution 2:[2]

Don't forget to register your video activity in your manifest by setting android:supportsPictureInPicture to true:

<activity android:name="VideoActivity"
android:supportsPictureInPicture="true"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joanne Chang
Solution 2 Célio Oliveira Gonzaga Júnior