android - launchMode=singleTask 和通知?

发布于 2024-10-24 00:30:01 字数 416 浏览 5 评论 0原文

我知道有一些帖子可以回答我要问的问题,但我找不到任何有正确答案的帖子。

根据我的理解,如果您的主要活动(我们称之为 A)的 launchMode 设置为 singleTask,并且 A 已启动活动 B,那么单击主页按钮将破坏历史堆栈,重新启动应用程序将带您返回 A而不是 B。

我将 launchMode 设置为 singleTask,因为我有一个持久通知,并且我不希望在用户单击通知时出现主活动的多个实例。

我是否缺少一些可以让我同时满足两者的东西?

所以我问是否有一种方法可以确保每当用户希望启动应用程序时,无论是否通过通知,都可以将他带回到最后一个(当前)活动。

如果我将 launchMode 更改为 singleTop,它可以工作,但每当启动它时,我都会获得主活动的多个实例。

谢谢安德烈亚斯

I know there's been a few posts for what I'm about to ask but I can't find any with the right answer.

From my understanding, if your main activity's (let's call it A) launchMode is set to singleTask, and A has initiated activity B then a click to the home button will destroy the history stack and re-launching the application will take you back to A and not B.

I have launchMode set to singleTask because I have a persistent notification and I don't want to have multiple instances of the main activity to appear whenever the user clicks on the notification.

Is there something I'm missing that would allow me to cater for both?

So I'm asking if there's a way I can ensure that whenever the user wishes to launch the app, from the notification or not, to take him back to the last (current) activity.

If I change launchMode to singleTop it works but I get multiple instances of the main activity whenever I launch it.

Thanks

Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜清冷一曲。 2024-10-31 00:30:01

您是否尝试过将应用程序中的所有活动的 launchMode 设置为 singleTop ?因为我从您的查询中得到的是主活动不是 singleTop,因此一旦从通知活动启动的活动启动主活动,可能会导致调用主活动的另一个实例。

或者,您可以将 launchMode 指定为清单中应用程序标记本身的属性。

Have you tried setting launchMode to singleTop to all the activities in your app?? Because what i get from your query is that the main activity isn't singleTop, so that might lead to another instance of the main activity being called once the main activity is launched from the activity that was launched from the notification activity.

Or you can specify the launchMode as an attribute to the application tag itself in the manifest.

决绝 2024-10-31 00:30:01

我使用以下代码来避免活动的多个实例

Intent intent=new Intent(this,RICO.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

更改清单看起来不适合我

I use the following code to avoid multiple instances of the activity

Intent intent=new Intent(this,RICO.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

Changing manifest doesn't look appropriate to me

简单 2024-10-31 00:30:01

我对这两种方法都有问题。
该通知仅在这种情况下才能完美运行:
- 在主活动中使用后退按钮(历史记录仅包含该活动)
- 不使用主页按钮
- 如果您调用的活动位于顶部且处于活动状态,则不使用通知

在任何其他情况下,通知都不能再使用“new Intent(...)”在前台调用活动

I'm having issues with both the approches.
The notification works flawless only in this condition:
- using the back button in the main activity (with the history containing only the that activity)
- not using the Home button
- not using the notification IF the activity you are calling is on top and active

In any other case, the notification cannot anymore call on the foreground the activity by using "new Intent(...)"

淡淡の花香 2024-10-31 00:30:01

我找到了清单选项和意图标志的炼金术组合,以获得我需要的东西:

Intent intent= new Intent(this, YaampActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

使用这些选项

android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"

在元素内

。现在我有一个通知,它会生成主要活动(如果该活动尚未在前台),并且即使通过按主页按钮和/或后退按钮“关闭”该活动,其行为也是正确的。

I've found the alchemical combination of manifest options and intent's flags for getting what I needed:

Intent intent= new Intent(this, YaampActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

using these options

android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"

inside the element.

Now I've a notification which spawns the main activity (if that activity is not already in the foreground) and its behavior is correct even if the activity is "closed" by pressing the home button and/or the back one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文