12

I have created a simple application having a button. Clicking it triggers a notification, and clicking on the notification launches a new instance of the same application. However, I wanted that clicking on the notification should bring me back to the application instance from which the notification was triggered. For this I consulted the Android docs for the FLAG_ACTIVITY_NEW_TASK flag-

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

Based on this when creating the intent for passing to the PendingIntent, i set this flag. However, clicking on the notification still launches a new instance of the application.

What am I doing wrong ?

Cygnus
  • 3,222
  • 9
  • 35
  • 65
  • Is the `Activity` you are starting already started in your app (when you hit the notification), or are you starting a different `Activity` in your app? – Steven Byle Apr 08 '13 at 14:55
  • @StevenByle : no its the same activity that i want to start - the one which is running and that triggered the notification. – Cygnus Apr 08 '13 at 14:56
  • Hmmm maybe post your `Intent` code... maybe it's something simple that we're missing... – Steven Byle Apr 08 '13 at 14:58
  • 2
    It is supposed to work as you've described. If it isn't doing that, I would bet that you have been bitten by a long-standing and very nasty Android bug. See my answer to [this question](http://stackoverflow.com/questions/15062957/android-application-apk) – David Wasser Apr 08 '13 at 15:08

3 Answers3

6

Remember that when you click the Notification it is from that Context that the intent is being launched. That context doesn't have the Activity on it's task (infact, it will be a blank task).

What this results in is two version of the same Activity (although still only one instance of you Application) running. Each Activity is running a different Task.

If you don't need duplicate Activities of the same type in any of your stacks you could use the answer here:

https://stackoverflow.com/a/2327027/726954

Otherwise, there are many ways to "fix" this problem, including singleton variables and Application Context methods that keeps track of which Activities are in a Running state.

You may need to search and refine your question for those.

Community
  • 1
  • 1
Graeme
  • 25,714
  • 24
  • 124
  • 186
  • Ok..but when we create the PendingIntent we specify the context in which to start the activity as the first argument to getActivity(). So shouldnt it be started according to that context ? Or am i missing something ? – Cygnus Apr 08 '13 at 15:59
  • Also, if the docs say "if a task is already running for the activity you are now starting, then a new activity will not be started", then in this case isnt it the case that the task for the activity is running ? – Cygnus Apr 08 '13 at 16:16
  • Do a bit of reading on `Task`s. Here is the Documentation on getActivity - http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int) - it says "Note that the activity will be started outside of the context of an existing activity" – Graeme Apr 08 '13 at 16:21
  • 1
    on one of the App's I use which use Notification i use getBroadcast() and have each of my Activities register Broadcast Receivers to handle calls from the Notification (and a default, backup receiver when there are no Activities to catch it). – Graeme Apr 08 '13 at 16:22
  • Thanks !! I find the docs to be quite confusing for some sections..basically for PendingIntent, we should use getApplicationContext() as the first argument right ? – Cygnus Apr 09 '13 at 14:02
  • You can use any `Context` you like, but from the documentation is seems it will ALWAYS be part of a new Task. – Graeme Apr 09 '13 at 14:39
  • @Graeme I used your suggestion with broadcast receivers and it works great for most phone but it seems like it doesn't work for some phones/android versions, I've not narrowed it down yet. Do you have any idea on what could cause this ? Specifically when the flag Intent.FLAG_ACTIVITY_NEW_TASK is included then it works for all phones but when it isn't then it only works for some phones, and clicking on the notifications doesn't do anything at all for some others.! – sakis kaliakoudas Jun 22 '15 at 15:32
  • @sakiskaliakoudas Try adding a random number parameter to your broadcast. Sometimes weird caching happens at the OS level which blocks your intents. – Graeme Jun 23 '15 at 13:52
  • @Graeme not sure I understand. The pending broadcast is delivered to my broadcast receiver, but after that when I call for the activity to start with FLAG_REORDER_TO_FRONT nothing happens. I've only seen this in android 4.4.2 devices – sakis kaliakoudas Jun 23 '15 at 14:10
  • in you Broadcast intent, try adding putExtra("RandomNumber", Math.rand() * 100000); I had an issue with some versions of Android thinking a Broadcast was a duplicate and so discarded it. Might not be the same thing but worth trying. – Graeme Jun 23 '15 at 14:16
1

A Task in Android is a separate User workflow. If you mange to see the Homescreen sometime, that usually means you start a new one. Remove the flag and it should work. if it does not, try using Single top.

meredrica
  • 2,563
  • 1
  • 21
  • 24
  • Umm i did not get it...when clicking the notification, the same instance that triggered my notification should appear. If i dont set any flag, a new instance is launched and i dont want that.. – Cygnus Apr 08 '13 at 14:54
  • 1
    Try using single top then. The thing you want to accomplish is actually quite tricky - I have code for this at home and can look it up later. – meredrica Apr 08 '13 at 14:55
  • @meredrica Would appreciate a lot some insights, to fix a similar problem. I ended using the manifest's `android:launchMode="singleTop"`, but it prevents the app to have a back-history when, i.e., I recycle the preference activity changing theme on-the-fly. Thanks. – dentex Feb 24 '14 at 14:34
0

Try the below code:

Intent resultIntent = new Intent(context, YourActivity.class);

resultIntent.setAction(Intent.ACTION_MAIN)       resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                resultIntent, 0);


 .setContentIntent(pendingIntent)
coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
Abhishek Kumar
  • 111
  • 1
  • 7