如何从通知状态栏打开最后一个活动? [英] How to open last activity from notification status bar?
问题描述
我想通过点击状态栏中的通知来打开上次启动的活动.假设我启动了一个活动 A(我的应用程序的主要活动),这个活动会向通知状态栏发送一个通知.活动 A 还打开活动 B,B 打开另一个活动 C.从 C 我按下主页按钮.现在我想再次转到我的应用程序,因此从通知栏中我点击通知(由 A 发送).此处通知应启动活动 C,因为它是上次打开的.
I want to open last started activity by tapping on the notification in status bar. Suppose I start an Activity A (main activity of my app), this activity sends a notification to notification status bar. activity A also opens an activity B and B opens another activity C. From C i press home button. Now i want to go again to my app so from notification bar i tap on notification (which was sent by A). Here the notification should start activity C because it was last opened.
我确实对此进行了搜索,但没有找到正确的答案.提前致谢.
I did search on this but didn't find proper answer. Thanks in advance.
推荐答案
几天前,我的问题得到了非常简单的解决方案.我们可以做如下简单的事情,而不是遍历 recentTasks
并获取我们的任务,然后通过它获取 baseIntent
:
Few days back I got very very simple solution for my problem. Instead of iterating through recentTasks
and getting our task and then getting baseIntent
through it, we can do simple thing as follows:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setAction(Intent.ACTION_MAIN);
baseIntent
包含与上述 Intent
相同的参数.因此,与其从 recentTasks
中获取 baseIntent
,不如使用上面的代码.
baseIntent
contains the same parameters as above Intent
has. Hence instead of grabbing baseIntent
from recentTasks
, it's quite good to use above code.
此 notificationIntent
然后将传递给 pendingIntent
以供进一步使用.
This notificationIntent
will then be passed to pendingIntent
for further use.
提供:MainActivity
是我们启动应用程序时的第一个活动,并且在 AndroidManifest.xml 中它必须包含 CATEGORY_LAUNCHER
的 IntentFilters
和 <代码>ACTION_MAIN.
Provided: MainActivity
is the very first activity when we launch our app and in AndroidManifest.xml it must contain IntentFilters
of CATEGORY_LAUNCHER
and ACTION_MAIN
.
这篇关于如何从通知状态栏打开最后一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!