为什么我们使用TaskStackBuilder? [英] Why do we use the TaskStackBuilder?

查看:95
本文介绍了为什么我们使用TaskStackBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在创建通知时使用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);
}

推荐答案

假设您有一个电子邮件发送应用程序,并且其中有两个活动.一个是MainActivity,它具有电子邮件列表,另一个是用于显示电子邮件(EmailViewActivity).因此,现在当您收到新的电子邮件时,您会在状态栏上显示一条通知.现在,您想在用户单击该电子邮件时查看该电子邮件,并且如果用户单击后退"按钮,则还要在显示该电子邮件之后查看您要显示电子邮件列表活动(MainActivity)的电子邮件.对于这种情况,我们可以使用TaskStackBuilder.参见以下示例:

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);
    }

希望你能理解.

按照以下网址获取更多详细信息: 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

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

这篇关于为什么我们使用TaskStackBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆