2

I raised a notification . What i want is , that when the user clicks on the notification , my activity is brought to the front without creating a new instance of it . For this , I added the flag , REORDER_TO_FRONT , but still oncreate is being called instead of onNewIntent when i click on the notification .

This is my code -

        int icon = R.drawable.android;
        long when = System.currentTimeMillis();
        CharSequence text = "new message";
        CharSequence contentTitle = stanza.getFrom();  // message title
        CharSequence contentText = stanza.getBody();      // message text

        Intent notificationIntent = new Intent(context, ChatBox.class);
        notificationIntent.putExtra("buddyid",stanza.getFrom());
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);


// the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, text, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1,notification);
Vinayak Bhavnani
  • 618
  • 1
  • 4
  • 10
  • visit http://stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foreground-top-of-stack That works fine for me! –  Dec 24 '13 at 08:01

2 Answers2

0

Have u tried:

Intent.FLAG_ACTIVITY_CLEAR_TOP

with your notificationIntent.addFlag();

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25
  • I tried it , right now , same result , oncreate is getting called . – Vinayak Bhavnani Sep 21 '12 at 12:04
  • Ok. Now try this one. Intent intent= new Intent(context, ChatBox.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); and in manifest add to your activity android:launchMode="singleTop" I think this will help you. – AnujMathur_07 Sep 21 '12 at 12:16
0

The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:

  1. Notification triggers a broadcast action with an extra the name of the activity to launch.

  2. Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag

  3. Activity is brought to the top of activity stack, no duplicates.

sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51