androidandroid-intentandroid-pendingintentandroid-picture-in-picture

Launching Intent from notification opening in picture-in-picture window


I've followed the Android picture-in-picture documentation. That is to say, my PiP activity is marked android:launchMode="singleTask", android:resizeableActivity="true", and android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" in the manifest.

Picture-in-picture seems to work great. However, I have noticed a case where I am experiencing undesirable behavior.

When my app receives a push, I create a PendingIntent with an Intent for MyActivity (different Activity than the PiP Activity). However, when I tap on the notification and the Intent is used, MyActivity is launching inside the PiP window!

I've tried adding android:launchMode="singleTask" to MyActivity in the manifest. I've also tried adding that flag programmatically to the Intent before using it with PendingIntent.getActivity(). This does not seem to fix the issue.

Can anyone tell me how to prevent this?


Solution

  • To address this issue, we should set a unique taskAffinity for the PiP Activity in manifest. It indicates the task which the activity is belonged to. Setting a unique value for it separates the task of PiP Activity from your MainActivity.

    <activity
        android:name=".VideoActivity"
        android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
        android:taskAffinity=".VideoActivity"
        android:supportsPictureInPicture="true" />
    


    ---------------------- Before ---------------------------------------------- After ------------------------

    enter image description here enter image description here