2

I start activity from notification with pending intent with back stack. Here is how I make it

TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addNextIntentWithParentStack(intent);
stackBuilder.editIntentAt(0).putExtra(GCMUtils.NEXT_INTENT,BaseFragment.FragmentId.DIALOGS).
        setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
resultPendingIntent = stackBuilder.getPendingIntent(requestCode, PendingIntent.FLAG_ONE_SHOT);

Then from activity A(started with intent) I start activity B and finish activity A

Bundle extras = activity.getIntent().getExtras();
boolean isFromNotification = extras != null && 
        extras.getBoolean(GCMUtils.NOTIFICATION_INTENT, false);
boolean noBackStack = isFromNotification && 
        Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB;
if (activity.isTaskRoot() || noBackStack) {
    Intent intent = activity instanceof ActionBarActivity ? 
            ((ActionBarActivity)activity).getSupportParentActivityIntent() : 
            NavUtils.getParentActivityIntent(activity);
    if (noBackStack) {
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    }
    activity.startActivity(intent);
    activity.finish();
} else {
    activity.onBackPressed();
}

After that I exit my app with back button (activity B is main activity). Now if open recent apps and click my app it will restore activity A instead of activity B. It reproduces on android 2.3. I don't understand why it restores A instead of B. What am I missing?

Asimaruk
  • 136
  • 7
  • CLARIFICATION! Effect is different when I use back button and home button. In case of home button it restores activity B as expected. Only in case of back button it restores activity A. Maybe it saves intent that starts application and reuses it when launched from recent apps? P.S. Application should be force stopped to reproduce this behaviour. – Asimaruk Dec 10 '14 at 20:20
  • Your statements make no sense. BACK AND HOME do completely different things When you press BACK, this finishes the current Activity and returns to the previous Activity on the stack. If there is no previous Activity, your app exits. However, pressing HOME doesn't call `finish()` on any Activity. It just moves your task to the background. When you bring an existing task from the background to the foreground (by selecting it from the list of recent tasks), your task should be in exactly the same state as it was when you left it (ie: same Activity should be on top). – David Wasser Dec 14 '14 at 18:32
  • Also see http://stackoverflow.com/questions/19813652/remove-data-from-notification-intent/19820057#19820057 maybe this explains what you are seeing. – David Wasser Dec 14 '14 at 18:34

1 Answers1

0

If you are running on an Android 2.3 device, you are using the compatibility library version of TaskStackBuilder. This constructs a back stack, but on Android < 3.0, it doesn't support the complete behaviour. If you look at the documentation for TaskStackBuilder it clearly states:

TaskStackBuilder provides a backward-compatible way to obey the correct conventions around cross-task navigation on the device's version of the platform. On devices running Android 3.0 or newer, calls to the startActivities() method or sending the PendingIntent generated by getPendingIntent(int, int) will construct the synthetic back stack as prescribed. On devices running older versions of the platform, these same calls will invoke the topmost activity in the supplied stack, ignoring the rest of the synthetic stack and allowing the back key to navigate back to the previous task.

I've highlighted the relevant part of the text in bold.

So, on a 2.3 device, when you launch the app from the Notification, there is no back stack. It just launches Activity A. Later, after you've quit the app (finished all activities in the stack), when you launch the app from the recent task list, it just starts the app the same way you started it from the notification: by launching Activity A.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I knew that I don't have back stack on android 3.0 and lower. If I press back button in activity A it will go to home screen. But I finish activity A and start another activity B. It really launches, I can clearly see this. And now why it still restores A? – Asimaruk Dec 10 '14 at 19:24