4

I have developed an application which has the functionality of receiving notifications from server.

The problem is, when I click on a notification that I have received, it opens a new instance of my application itself.

This behavior is ok, if my app is not in the foreground, but if it is and I try to open a notification, a new instance of my app is created and thus overlapping the previously opened instance of the app.

I don't want this to happen, so when I click on the notification if my app is in the foreground I have to close that and open a new instance.

How should I override the notification's click event?

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • I have exactly the same problem. For now I'm terminating the non-visible instance when I have several instances of my Activity running. It works, but I'm quite sure this is not the best way at all. – Shlublu Sep 05 '11 at 13:25
  • @Shlublu - Can you give me a little guidance on how this can be done. Bcoz I am totally blank here. – Andro Selva Sep 05 '11 at 13:27
  • 1
    I think the following link can help you http://stackoverflow.com/questions/2326622/starting-activity-through-notification-avoiding-duplicate-activities – Yashwanth Kumar Sep 05 '11 at 13:28
  • @YashwanthKumar Thank you! Andro, I propose seeing whether Yashwanth Kumar's solution works (it seems so) first, as it is way more clean. – Shlublu Sep 05 '11 at 13:30
  • @Yashwanth Kumar No this didn't help. I am using it in my TabAcitivity and before this I have a splash screen also. So when I receive a notification I am redirecting it to my first tab. but still I can see my previous instance being overlapped. – Andro Selva Sep 05 '11 at 13:46
  • Ok, so in that case, let's see the workaround: I maintain a list of the running instances of my `Activity` and similar to the one you can see in that post: http://stackoverflow.com/questions/7254720/how-to-restart-an-activity-automatically-after-it-crashes/7255397#7255397 - when my `Activity.onCreate()` is called, I call `terminate()` on any pre-existing instance. Not very nice, and this is why I would like to find a better way too. – Shlublu Sep 05 '11 at 13:53
  • its ok my friend. I have made use of the answer suggested my balaji and its all fine. Thanks for your time. – Andro Selva Sep 05 '11 at 13:57
  • A great! And I will do the same use of it :) – Shlublu Sep 05 '11 at 14:29

4 Answers4

5

you need to do some magic with the IntentFlags. try to add different flags to your intent.

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
Balaji.K
  • 8,745
  • 5
  • 30
  • 39
  • hey but its not updating the values from server. any ideas on it? – Andro Selva Sep 05 '11 at 14:07
  • while creating the pendingIntent,set the flags parameter to PendingIntent.FLAG_UPDATE_CURRENT .then ur new values will be updated – Balaji.K Sep 05 '11 at 14:18
  • 1
    see this code PendingIntent pintent = PendingIntent.getService(this, 0, urintent, PendingIntent.FLAG_UPDATE_CURRENT); – Balaji.K Sep 05 '11 at 14:20
  • ya ya got it. I just used Intent.FLAG_ACTIVITY_CLEAR_TOP alone. It solved it. – Andro Selva Sep 05 '11 at 14:32
  • @AndroSelva How will you solved it exactly? I have tried many combinations even adding in AndroidManifiest activities "android: launchmode" but can't get the new initiative launched by clicking on the notification close or replace the open application . – ephramd Jul 17 '13 at 20:36
1

you can add flag in intent while setting the intent to pending intent like this:

Intent notificationIntent = new Intent(context, activity.class);
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • No this didn't help. I am using it in my TabAcitivity and before this I have a splash screen also. So when I receive a notification I am redirecting it to my first tab. but still I can see my previous instance being overlapped. – Andro Selva Sep 05 '11 at 13:45
  • you can add android:launchMode = "singleInstance" to your activity tag in manifest file... – Vineet Shukla Sep 05 '11 at 13:47
0

If you use action MAIN and category LAUNCHER in your intent, it will resume your existing instance. It is same as you launch your aplication from launcher or last used applications. Probably category LAUNCHER is not necessary.

Salw
  • 1,880
  • 17
  • 22
  • the problem is, it overlaps as usual in this case and additionally my app is not launched automatically which the customer won't like. But thanks for your suggestion bro. – Andro Selva Sep 05 '11 at 13:59
-1
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

In this way you will clear your last instance along with all the activities and launch a new instance of the application.

Galma88
  • 2,398
  • 6
  • 29
  • 50
Farina Ali
  • 11
  • 5