从通知中恢复活动

maz*_*box 35 java notifications android android-activity

我的应用状态栏中有通知:

    Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis());

    Intent notificationIntent = new Intent(this.parent, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this.parent, 0, notificationIntent, 0);

    ...

    notification.flags = Notification.FLAG_ONGOING_EVENT;        
    mNotificationManager.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

这个问题是当你从应用程序按下主页按钮(将其推到后台),然后按下从状态栏访问的列表中的通知,它会启动活动的新副本.我想要做的就是恢复应用程序(比如当你长按主页按钮并按下应用程序的图标时).有没有办法创建一个Intent来做到这一点?

rek*_*eru 32

我已经通过在androidManifest.xml文件中更改launchMode我的活动来解决了这个问题singleTask.

此属性的默认值为standard,允许运行任意数量的实例.

"singleTask"和"singleInstance"活动只能开始一项任务.它们始终位于活动堆栈的根部.此外,设备一次只能保存一个活动实例 - 只有一个这样的任务.[...]

"singleTask"和"singleInstance"模式在一个方面也各不相同:"singleTask"活动允许其他活动成为其任务的一部分.它始终是其任务的根源,但其他活动(必然是"标准"和"单一活动")可以启动到该任务中.另一方面,"singleInstance"活动不允许其他活动成为其任务的一部分.这是任务中唯一的活动.如果它启动另一个活动,则该活动将分配给另一个任务 - 就像FLAG_ACTIVITY_NEW_TASK在意图中一样.

您可以在Android开发人员指南中找到详细说明

我希望这有帮助