Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use the TaskStackBuilder?

Why do we use the TaskStackBuilder when creating a notification? I do not get the logic behind it.

Can someone please explain.

public void showText(final String text){     Intent intent = new Intent (this, MainActivity.class);     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);     stackBuilder.addParentStack(MainActivity.class);     stackBuilder.addNextIntent(intent);     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); } 
like image 352
MisterNowhere Avatar asked Apr 28 '16 10:04

MisterNowhere


1 Answers

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

like image 140
Tharindu Welagedara Avatar answered Sep 23 '22 08:09

Tharindu Welagedara