FLAG_ACTIVITY_REORDER_TO_FRONT is ignored

I have a FragmentActivity with a list of elements, and when the application is in the background, there may be a click on this list of elements.

when this happens, I want to create a notification in the status bar and notify the user about the update.
When the user clicks on the notification, the action should be reordered forward and show on the screen when showing a new item at the bottom of the list.

so I write a notification manager that shows what is on the user 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 problem is that it seems to ignore my flag when I go to fragment activity and then go to the main screen (background application) and get push, when I click on the notification, the application creates a new activity (or fragment) and shows a new, not original, with new data. (this means that pressing the "Back" button opens the same activity (or fragment) from the history stack.

I redefined onNewIntent and all the lifecycle methods of this activity, and I saw that on the callback, the methods that are called are MyFragmentActivity.onStart and MyFragmentActivity.onResume .

Any thoughts on what I'm doing wrong?

+4
source share
2 answers

I assume the problem is that you set the activity start mode to singleTask, which adds the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag to your intentions and this will ignore the FLAG_ACTIVITY_REORDER_TO_FRONT flag.

you need to change the startup mode to one top.

+5
source

A decision marked as accepted is erroneous if there is no special scenario that I am following, and you are not. If you have launchmode = "singleTop" and you have a notification that opens activity A and your application is currently looking at activity B (the application is minimized or maximized, that is, in the background or foreground it does not matter ) Then even if the FLAG_ACTIVITY_REORDER_TO_FRONT Flag has been set, it will still open a second copy of activity A.

The solution for me was to make a broadcast receiver that listens for broadcast actions triggered by a notification. So basically:

  • The notification triggers the translation action with the additional name of the operation being launched.

  • The broadcast receiver catches this when the notification is clicked, and then creates the intention to trigger this action using the FLAG_ACTIVITY_REORDER_TO_FRONT flag

  • Activity is displayed at the beginning of the action stack, there are no duplicates.

+1
source

All Articles