Why do we use the TaskStackBuilder
when creating a notification? I do not get the logic behind it.
为什么我们在创建通知时使用TaskStackBuilder?我没有得到它背后的逻辑。
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);
}
3 个解决方案
#1
46
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 an 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:
假设您有一个电子邮件发送应用程序,并且您有两个活动。一个是MainActivity,它有电子邮件列表,另一个是显示电子邮件(EmailViewActivity)。现在,当您收到新电子邮件时,您会在状态栏上显示通知。现在,您希望在用户点击该电子邮件时以及在用户单击后退按钮后显示该电子邮件时查看该电子邮件,以显示电子邮件列表活动(MainActivity)。对于这种情况,我们可以使用TaskStackBuilder。见下面的例子:
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
请按照以下网址获取更多详细信息: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
#2
4
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.
我们使用TaskStackBuilder确保后台按钮在活动开始时可以很好地播放。 TaskStackBuilder允许您访问后退按钮使用的活动历史记录。基本上,当我们希望用户在按下后退按钮后导航到另一个活动时,我们会使用它。
#3
1
The other answers explained it nicely: you use a pending intent to send a user into a detail activity, then you want them to use the back button to go back to the main activity. An alternative way to set this is
其他答案很好地解释了:您使用待定意图将用户发送到详细活动,然后您希望他们使用后退按钮返回主活动。另一种设置方法是
Intent detailIntentForToday = new Intent(context, DetailActivity.class); TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context); taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday); PendingIntent resultPendingIntent = taskStackBuilder .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
For this, you also need to set
为此,您还需要设置
android:parentActivityName=".MainActivity"
for the DetailActivity
in AndroidManifest.xml
.
对于AndroidManifest.xml中的DetailActivity。
#1
46
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 an 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:
假设您有一个电子邮件发送应用程序,并且您有两个活动。一个是MainActivity,它有电子邮件列表,另一个是显示电子邮件(EmailViewActivity)。现在,当您收到新电子邮件时,您会在状态栏上显示通知。现在,您希望在用户点击该电子邮件时以及在用户单击后退按钮后显示该电子邮件时查看该电子邮件,以显示电子邮件列表活动(MainActivity)。对于这种情况,我们可以使用TaskStackBuilder。见下面的例子:
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
请按照以下网址获取更多详细信息: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
#2
4
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.
我们使用TaskStackBuilder确保后台按钮在活动开始时可以很好地播放。 TaskStackBuilder允许您访问后退按钮使用的活动历史记录。基本上,当我们希望用户在按下后退按钮后导航到另一个活动时,我们会使用它。
#3
1
The other answers explained it nicely: you use a pending intent to send a user into a detail activity, then you want them to use the back button to go back to the main activity. An alternative way to set this is
其他答案很好地解释了:您使用待定意图将用户发送到详细活动,然后您希望他们使用后退按钮返回主活动。另一种设置方法是
Intent detailIntentForToday = new Intent(context, DetailActivity.class); TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context); taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday); PendingIntent resultPendingIntent = taskStackBuilder .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
For this, you also need to set
为此,您还需要设置
android:parentActivityName=".MainActivity"
for the DetailActivity
in AndroidManifest.xml
.
对于AndroidManifest.xml中的DetailActivity。