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

Viewed 2244

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?

4 Answers

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

First of all, as you can see in google sample codes, it does not need to make the launch mode of PIP Activity singleTask. However, you need to move the launcher task to front which is not in PIP mode, in order to disabling PIP state.

By calling bellow snippet in the onCreate() method of the target Activity which is called from notifications, you can achieve this.

public static void moveLauncherTaskToFront(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    assert activityManager != null;
    final List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
    for (ActivityManager.AppTask task : appTasks) {
        final Intent baseIntent = task.getTaskInfo().baseIntent;
        final Set<String> categories = baseIntent.getCategories();
        if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
            task.moveToFront();
            return;
        }
    }
}

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

enter image description here enter image description here

add android:supportsPictureInPicture="false" to MyActivity in the manifest. It should fix your problem

I've also met this problem, and you can try to add "android:excludeFromRecents="true" in your MyPipActivity manifest

Related