'Android 12 notification/widget PendingIntents show other activity after closing

After updating my device to android 12, activities that I start from notifications or widgets using a PendingIntent will show another activity that was started before (like the launcher activity) after closing that activity.

I've tried using FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TASK or FLAG_ACTIVITY_SINGLE_TOP but that doesn't seem to work.



Solution 1:[1]

You need to set the activity launch mode.

 android:launchMode=["standard" | "singleTop" |
                          "singleTask" | "singleInstance" | "singleInstancePerTask"]

In your case, you will need to set it to single instance to prevent that problem.

android:launchMode="singleInstance"

You can check more about setting up activites here -> element

Solution 2:[2]

Using android:launchMode="singleInstance" on the activity in AndroidManifest.xml seems to solve the issue

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 David Kariuki
Solution 2 0ne_Up