3

I am in a problem with the notifications in android, whenever I click the notification evrytime I have to call the same activity again, but as far as I am thinking the new activity is called but the previous is also running in the backend, due to which my code is running again and again ( becoz of multiple instances )

Please help me how to resolve or close multiple instances each time a notification is clicked.

code

public void notificationforChat(CharSequence message,String toJid, int notificationID) {

    int notificationCount = 1;
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = message;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    //notification.number = notificationCount++;
    Context context = getApplicationContext();



    CharSequence contentTitle = "Chat";
    CharSequence contentText = message;
    Intent notificationIntentforChat = new Intent(this, UserChatActivity.class);
    notificationIntentforChat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationIntentforChat.putExtra("userNameVal", toJid);       
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntentforChat, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, contentTitle, contentText,
            contentIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults = Notification.DEFAULT_ALL;   
    mNotificationManager.notify(notificationID, notification);
}

thanks

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • You mean single activity only in your app? –  Nov 16 '12 at 05:53
  • Yes. suppose you are on your gtalk chat screen, chatting with someone else and now if another person pings you then you will be notified via notification, now when you click that notification the other person;s chat screen will open. Now here I the previous person's chat screen is also running in the background – Gaurav Arora Nov 16 '12 at 05:56

4 Answers4

1

Put the below code for that activity in maifest :

android:launchMode="singleTop"

Then for handling the new changes when the activity is called again, override the following method.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    //Do your new intent handling here.     
}

The code you put in manifest makes sure that only one instance of that activity is created. and you can handle the changes required on new intent in the onNewIntent overrided method in that activity.

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • The problem is this when I am opening the new window the same screen is opening, the new screen is not opening. wat's the problem?? – Gaurav Arora Nov 16 '12 at 06:23
1

To prevent multiple instances of activity.

You can use android:launchMode="singleTask" in your activity

<activity
            android:launchMode="singleTask"
            android:name=".UserChatActivity"
            android:label="Your title" > 

....
</activity>

Once you call it. The onNewIntent(Intent) will be triggered with a new intent

    @Override
    protected void onNewIntent(Intent intent) {

       super.onNewIntent(intent);      


    }
  • The problem is this when I am opening the new window the same screen is opening, the new screen is not opening. wat's the problem?? – Gaurav Arora Nov 16 '12 at 06:22
  • Thats y I told you to make use of the overrided onNewIntent Method. In that method, update the view with new values that you need to show. – Eldhose M Babu Nov 16 '12 at 06:25
1

What about starting the activity in android:launchMode="singleInstance" mode or android:launchMode="singleTop" mode. I think this helps.

Chandrashekhar
  • 498
  • 4
  • 19
0

Add to your calling intent

mIntent. setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Add to the activity in menifest

android:launchMode="singleTop"
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95