安卓:通知上点击时总是推出顶级活动通知

2023-09-12 23:45:48 作者:初雪未霁

修改:请注意:我完全重新解释我的问题

EDIT: please note: i completely re-explained my question.

我的应用程序有两个 Activites : A 和 B 。 活动 A 是。因此,应用程序启动,和 A 出现在屏幕上。 PSS上的按钮,用户$ P $,而新的活动 B 出现在屏幕上。

I have application with two Activites: A and B. Activity A is MAIN. So, application starts, and A appears on the screen. User press the button on it, and new Activity B appears on the screen.

所以,现在有2活动,在我的回栈: A 和 B

So, there's now 2 activities in my "back stack": A and B.

现在我preSSHome键,然后点击在启动我的应用程序的图标:活动 B 出现在屏幕上(不是),就因为它是在我的任务顶。

Now I press "Home" key, and then click on my app's icon on launcher: Activity B appears on the screen (not A), as far as it is top activity in my task.

现在的问题:如何才能让意图以同样打开当前的顶活动

Now question: how can i make Intent to similarly open the currently top Activity of my task?

我需要它通知来使用:当用户点击我的通知,顶部活动这个任务应该出现在屏幕上,而不是指定的一个。

I need it to use in Notification: when user clicks on my Notification, top Activity of this task should appear on the screen, not a specified one.

我试了很多意向标志,如 SINGLE_TOP 和其他人,但我仍然不能得到我所需要的。

I tried many Intent flags, such as SINGLE_TOP and others, but i still can't get what i need.

有谁知道该如何解决?

推荐答案

好了,花几个小时,过了些日子,我找到了解决办法:

Well, after spending several hours some days, i found the solution:

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

这是它启动启动应用程序的方式,和它的作品不错的通知意向了。

That's the way which launcher starts applications, and it works nice for notification Intent too.