i am using this solution : How to make notification intent resume rather than making a new intent?
I works fine when i run my app normally.. however my app has a share function.
When i select the images i want to share from gallery app and it open it in my activity, i create a notification in the activity. I want that when user clicks on the notification it opens the existing activity (opened by the gallery app)
The problem is that when i click on the notification it does not resume that activity but instead open a new instance of the activity or another instance already opened previously
Edit:
More explanation
My app is called ShareApp and the activity is called ShareActivity
the problem is that when i open the ShareActivity via gallery app, an instance of the ShareActivity is created at the top of the gallery app task's stack. Now when i create a notification that points to my ShareActivity, i use the intent:
Intent intent = new Intent(this, ShareActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
problem is that when i click on the notification, it points to the instance of the ShareActivity in the the ShareApp task's stack instead of the gallery app task's stack..
any idea how to point to the correct task's stack??
edit 2: my code
int defaults = Notification.FLAG_NO_CLEAR;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setDefaults(defaults);
Intent intent = new Intent(this, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(_ID, mBuilder.build());
Edit 3 : adb shell dumpsys activity (after applying the code by David https://stackoverflow.com/a/16901603/1334268)
My app is called shareApp and my activity is the ShareActivity
Before clicking on notification:
Main stack:
TaskRecord{41965708 #6 A com.android.gallery3d}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
After clicking notification:
Main stack:
TaskRecord{41965708 #6 A com.android.gallery3d}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
Hist #3: ActivityRecord{4169f358 com.android.gallery3d/.app.Gallery}
Intent { flg=0x20000000 cmp=com.android.gallery3d/.app.Gallery bnds=[0,205][480,301] }
ProcessRecord{4175dd28 5808:com.android.gallery3d/10036}
Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
See Question&Answers more detail:
os