从通知中恢复活动 [英] resuming an activity from a notification

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

问题描述

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

 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, 通知);

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

解决方案

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

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

<块引用>

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

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

您可以在 Android 开发者指南中找到详细说明/p>

希望能帮到你

I have a notification in the status bar for my app:

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

The problem with this is that when you press the home button from the app (pushing it to the background) then press on the notification in the list accessed from the status bar, it starts a fresh copy of the activity. All I want to do is resume the app (like when you longpress the home button and press on the app's icon). Is there a way of creating an Intent to do this?

解决方案

I've solved this issue by changing the launchMode of my activity to singleTask in the androidManifest.xml file.

The default value for this property is standard, which allows any number of instances to run.

"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task. [...]

The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.

you can find a detailed explanation in the Android Developers' Guide

I hope this helps

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

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