Why do we use the TaskStackBuilder?

We use a TaskStackBuilder to make sure that the back button will play nicely when the activity gets started. The TaskStackBuilder allows you to access the history of activities used by the back button. Basically, we use it when we want the user to navigate to another activity after pressing back button.


To provide proper navigation.

1) When the app is launched by App icon (Normal Flow)

enter image description here **

2) When the app is launched by some Notification

enter image description here

General flow of navigation in your app is MainActivity->DetailActivity

But sometimes a Notification might directly open the DetailActivity. In this case, pressing the back button in DetailActivity will not lead you to the `MainActivity. It's an EXPECTED BEHAVIOR. However, you can modify this if you want to navigate back to MainActivity.

How do I do it?

1) Add android:parentActivityName="com.example.myApp.MainActivity in your Activity

This feature was added in Android 4.1. So if you want to target older devices. Add a meta-tag ALSO.

<activity android:name=".Activities.DetailActivity"
    android:parentActivityName=".Activities.MainActivity">
    <meta-data
      android:name="android.support.PARENT_ACTIVITY"
      android:value=".Activities.MainActivity" />
</activity>

2) Use TaskStackBuilder to create a Pending Intent.

public PendingIntent getPendingIntent(Intent intent){

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
    taskStackBuilder.addNextIntentWithParentStack(intent);
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

}

Now pass this Pending intent to create Notifications.


Suppose you have an email sending app and you have two activities in it. One is MainActivity which has the email list and other one is for displaying an email (EmailViewActivity). So now when you receive a new email you display a notification on statusbar. And now you want to view that email when a user clicks on it and also after displaying the email if the user clicks back button you want to show the email list activity(MainActivity). For this scenario we can use TaskStackBuilder. See below example:

public void showEmail(final String text){

        Intent intent = new Intent (this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(intent);
        Intent intentEmailView = new Intent (this, EmailViewActivity.class);
        intentEmailView.putExtra("EmailId","you can Pass emailId here");
        stackBuilder.addNextIntent(intentEmailView);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.app_name))
                .setAutoCancel(true)
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setContentIntent(pendingIntent)
                .setContentText(text)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICACTION_ID, notification);
    }

Hope you can understand.

Follow below urls for more details: http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html http://developer.android.com/guide/components/tasks-and-back-stack.html http://www.programcreek.com/java-api-examples/index.php?api=android.app.TaskStackBuilder