6

I am trying to refresh the contents of an activity on click of a notification. I can navigate to the activity when I am in some other activity and I click on the notification. What I am trying to achieve is, I am in Activity A which is displaying some content. I get a new notification, I click on it Activity A should either be relaunched or the content in the activity should be refreshed with respect to what I am passing in the PendingIntent of the Notification.

What all I have done,

  1. Tried setting PendingIntent.FLAG_CANCEL_CURRENT and PendingIntent.FLAG_UPDATE_CURRENT

  2. Tried setting Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP in the intent I am passing along with the pending intent.

  3. Checked the data in onNewIntent() its doesn't get refreshed. I get the same data which I have passed in the old intent.

  4. Passed a unique requestCode along with the PendingIntent as well, still the same.

Any Other suggestions?

iZBasit
  • 1,314
  • 1
  • 15
  • 30

4 Answers4

6

If you use Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP, this should recreate the activity if it was in the stack, or start a new activity if it wasn't in the stack. If you don't want the activity to be recreated (if it is already in the stack), you can use Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Your answer should be the right answer. It is actually enough to use only `Intent.FLAG_ACTIVITY_CLEAR_TOP` to recreate the activity if it was already in the stack. This flag's docs states: "If it (the target activity) has declared its launch mode to be "multiple" (the default (was changed to "standard")) and you have not set `FLAG_ACTIVITY_SINGLE_TOP` in the same intent, then it (activity) will be finished and re-created;". I tested it and it is indeed true! :) Any special reason why I should have also `Intent.FLAG_ACTIVITY_NEW_TASK` if I'm not interested in creating additional task? – Nimrod Dayan Aug 21 '16 at 17:18
  • 1
    Technically you should have `Intent.FLAG_ACTIVITY_NEW_TASK` if you are launching the `Activity` from a non-activity `Context`. In the case described, OP is creating a `Notification` and therefore the `Activity` will be launched from a non-activity `Context`. Android is a bit forgiving about this, but if you look in the logcat you should see a warning if you don't specify this flag in a `Notification`. Using this flag in this way doesn't actually create a new task if there is already a task containing this app. It actually launches the `Activity` into the existing task (if there is one). – David Wasser Aug 21 '16 at 19:00
4

As aldoran said, use LocalBroadcastManager. In your Activity class:

@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(FILTER_STRING));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d("receiver", "Got message");
  }
};

And then in GSM Broadcast put your data in intent:

Intent intent = new Intent(FILTER_STRING);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
manao
  • 328
  • 5
  • 12
  • 1
    Why in the world should/would I use broadcast receiver and send a broadcast I don't understand! – iZBasit Sep 26 '14 at 11:08
  • Sorry, I inattentively read the question. What is launchMode of Activity A? Try to set singleTop in Manifest. – manao Sep 26 '14 at 11:52
  • It is SingleInstance as of now. Have tried the same with SingleTop. Doesn't work. – iZBasit Sep 27 '14 at 13:33
1

try this code:

           Intent notificationIntent = new Intent(context, DetailActivity.class);
           notificationIntent.putExtra(CommonConstants.EXTRA_NOTE_ID, note.get_id());
           notificationIntent.setAction(CommonConstants.NOTE_NOTIFICATION_REMINDER);

           notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
           PendingIntent contentIntent = PendingIntent.getActivity(context, note.get_id(), notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
           NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(context.getString(R.string.app_name)).setContentText(note.getMessage());
           mBuilder.setContentIntent(contentIntent);
           mBuilder.setDefaults(Notification.DEFAULT_SOUND);
           mBuilder.setAutoCancel(true);
           NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
           mNotificationManager.notify(note.get_id(), mBuilder.build());

most important you have to call

 PendingIntent contentIntent = PendingIntent.getActivity(context, note.get_id(), notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

or

PendingIntent.FLAG_CANCEL_CURRENT

Min2
  • 10,751
  • 2
  • 19
  • 22
-1

You could also use LocalBroadcastManager or write your own BroadcastReceiver and then send Broadcast from your Notification

aldorain
  • 790
  • 5
  • 16
  • Excuse me? What are you saying? I am already getting a broadcast from GCM and am handling it in a Service and showing the message in a notification. What I want is to handle the activity cycle on notification click! – iZBasit Sep 26 '14 at 07:55