click on notification to go current activity
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] }
Notification.Builder mBuilder =
new Notification.Builder(this)
.setSmallIcon(R.drawable.cmplayer)
.setContentTitle("CoderoMusicPlayer")
.setContentText("PLayer0!");
Intent resultIntent = new Intent(this, AndroidBuildingMusicPlayerActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, 0);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
Just Copy the code and paste it in your main launcher activity.
If you have multiple instances of ShareActivity
active in different tasks, there is no way for you to tell Android which task to reactivate by launching your activity. It sounds to me like what you want to do, in this case, is to launch not your activity, but the Gallery application. If the Gallery app is already running and your activity is on the top of the task stack, then all you need to do is to bring the Gallery app to the foreground. To do this, create a "launch Intent" for the Gallery app and make sure to set Intent.FLAG_ACTIVITY_NEW_TASK
on the Intent. You should be able to get a "launch Intent" from the PackageManager
by using getLaunchIntentForPackage()
EDIT: Example code using ActivityManager
You might try this:
// Get the root activity of the task that your activity is running in
ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ActivityManager.RunningTaskInfo task = tasks.get(0); // Should be my task
ComponentName rootActivity = task.baseActivity;
// Now build an Intent that will bring this task to the front
Intent intent = new Intent();
intent.setComponent(rootActivity);
// Set the action and category so it appears that the app is being launched
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
NOTE: You will need GET_TASKS
permission for this. I haven't tried this and can't guarantee it will work. The use of getRunningTasks()
is discouraged by the documentation, but that doesn't mean that it won't suit your purposes.
EDIT added action/category to the Intent
NOTE: I actually built a small app to test this. I needed to add the ACTION=MAIN and CATEGORY=LAUNCHER to the Intent (which would be there if you used PackageManager.getLaunchIntentForPackage()
. I've got an HTC One S, so on my device the "Gallery" app is actually called com.htc.album/.AlbumMain.ActivityMainDropList
, but this should still work for you.
Set
android:launchMode="singleTop"
in your manifest.xml
for your main activity and see if that helps.