Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLAG_ACTIVITY_REORDER_TO_FRONT ignored

I have a FragmentActivity with a list of items, and when the app is in background, there can be a push to that item list.

when this happens, I want to create a statusbar notification and alert the user on the update.
when the user clicks the notification, the activity should reorder to front and show on the screen while showing the new item at the bottom of a list.

so I write a notification manager that shows that on the users device:

private static void createNotification(String title, String text,
    String largeIcon, String itemdId, Context mCOntext) {

Bitmap ic = BitmapFactory.decodeResource(mContext.getResources(),
    R.drawable.ic_launcher);
Intent intent = new Intent(mContext, MyFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra(MyFragmentActivity.SELECTED_ITEM_LIST_ID, DatabaseHelper
    .getItemListById(listId).getId());
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent,
    0);

Notification noti = new NotificationCompat.Builder(mContext)
    .setContentTitle(title).setContentText(text)
    .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
    .setAutoCancel(true).setLargeIcon(ic).build();
noti.defaults |= Notification.DEFAULT_LIGHTS;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.sound = Uri.parse("android.resource://"
    + mContext.getPackageName() + "/" + R.raw.user_gets_message);

NotificationManager nm = (NotificationManager) mContext
    .getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
}

The only issue is that it seems to ignore my flag, when I navigate to the fragment activity, and then go to my home screen (background the app), and get a push, when I click on the notification, the app creates a new Activity (or fragment) and shows the new one instead of the original with the new data. (this means that clicking the back button opens the same activity (or fragment) from the history stack.

I overridden the onNewIntent and all the life cycle methods of the activity and I saw that on back clicked, the methods that are called are the MyFragmentActivity.onStartand the MyFragmentActivity.onResume.

any thoughts on what it is that I'm doing wrong?

like image 844
thepoosh Avatar asked Feb 17 '23 18:02

thepoosh


2 Answers

i assume that the problem is that you set the activity launch mode to be singleTask which add the flag FLAG_ACTIVITY_BROUGHT_TO_FRONT to your intent, and it cause the flag FLAG_ACTIVITY_REORDER_TO_FRONT to be ignored.

you need to change the launch mode to single top.

like image 55
chezi shem tov Avatar answered Feb 27 '23 14:02

chezi shem tov


The solution marked as accepted is flawed, unless there's a special scenario that I am following and you aren't. If you have launchmode = "singleTop" and you have a notification that opens activity A and at your app is at the moment looking at activity B (app either minimized or maximized i.e. in background or foreground it doesn't matter) Then even though the FLAG_ACTIVITY_REORDER_TO_FRONT flag was set, it will still open a second copy of activity A.

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.

like image 44
sakis kaliakoudas Avatar answered Feb 27 '23 14:02

sakis kaliakoudas