意图恢复先前暂停的活动(从通知中调用) [英] Intent to resume a previously paused activity (Called from a Notification)

查看:29
本文介绍了意图恢复先前暂停的活动(从通知中调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个向用户显示通知的应用.通知的目的是让用户在用户处于另一个活动中时可以轻松地返回到该活动.我在我的应用中使用此代码来创建和显示通知.

I'm developing an app which shows a notification to the user. The notification's objective is to make it easy to the user to return to the activity when the user is in another activity. I am using this code in my app to create and show the notification.

                    notification = new Notification(R.drawable.icon,
                            "Notify",
                            System.currentTimeMillis());
                    notification.setLatestEventInfo(this, "App name",
                            "App message",
                            PendingIntent.getActivity(
                                    this, 0,
                                    new Intent(this, Main.class),
                                    PendingIntent.FLAG_CANCEL_CURRENT));
                    notification.flags |= Notification.FLAG_ONGOING_EVENT;
                    nManager.notify(0, notification);

但是当用户点击通知时,会启动相同活动的新实例,而不是用户之前使用的实例.

But when the user taps the notification starts a new instance of the same activity, instead of the one that the user was using before.

我认为这与 PendingIntent 有关,但我找不到如何使该 Intent 恢复先前暂停的 Activity 实例而不是创建新实例.

I think this has something to do with PendingIntent but I can't find how to make that Intent to resume a previously paused instance of the activity instead of creating a new instance.

谢谢.

推荐答案

我找到了方法.我添加了以下代码:

I found out how to do it. I added the following code:

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

现在我的代码是这样的:

Now my code looks like this:

    notification = new Notification(R.drawable.icon,
            "Notify", System.currentTimeMillis());
    notification.setLatestEventInfo(this, "App name",
            "App message", PendingIntent.getActivity(this,
                    0, new Intent(this, Main.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                    | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

这篇关于意图恢复先前暂停的活动(从通知中调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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