简体   繁体   English

从Android通知栏启动应用。 如何启动应用程序的新实例而不是恢复后台进程?

[英]Launching app from Android Notification Bar. How to start a fresh instance of application rather than resume background process?

I managed to get a notification from our app to appear on the Notification bar at the top of the screen, whenever it receives a push. 每当收到推送请求时,我都设法从我们的应用程序获得通知,以显示在屏幕顶部的通知栏上。 My application is a hierarchy of views. 我的应用程序是视图层次结构。 I have my main screen with a listview. 我的主屏幕带有列表视图。 When one of these listview items is clicked it opens a summary screen (also containing a listview), and when one of the summary screen listview items is clicked it opens a details screen (last screen in the hierarchy). 单击这些列表视图项之一时,它会打开一个摘要屏幕(也包含一个列表视图),而单击摘要屏幕列表视图项中的一个时,它将打开一个详细信息屏幕(层次结构中的最后一个屏幕)。 The user can hit the back button to travel back up the hierarchy to the main screen (or home screen). 用户可以单击“后退”按钮将层次结构返回到主屏幕(或主屏幕)。

Now, my application, and most apps for that matter, tend to run in the background until the back button is hit enough times. 现在,我的应用程序以及与此相关的大多数应用程序倾向于在后台运行,直到后退按钮被按下足够的次数为止。 I set my Notification to launch an instance of mainScreen when it is clicked. 我将Notification设置为在单击mainScreen的实例时启动它。 The thing is if the app is already running and I do this it creates another main screen over all the other screens, so when the user hits the back button on the main screen (expecting to exit out of the app) he'll go back to whatever screen was active when he left my app running in the background. 问题是,如果该应用程序已经在运行,那么我会在所有其他屏幕上创建另一个主屏幕,因此,当用户单击主屏幕上的“后退”按钮(希望退出该应用程序)时,他将返回当他让我的应用在后台运行时,无论屏幕处于活动状态。

So, I could potentially go back up the hierarchy like this: mainScreen -> detailsScreen -> summaryScreen -> mainScreen -> EXIT 因此,我可能会像这样备份层次结构:mainScreen-> detailsS​​creen-> summaryScreen-> mainScreen-> EXIT

Is there a way I can just kill the application and start a fresh instance when the user clicks my notification? 当用户单击我的通知时,有没有办法我可以杀死应用程序并启动一个新实例? How are these notification clicks normally handled? 通常如何处理这些通知点击?

Hope this makes sense. 希望这是有道理的。

Thanks. 谢谢。

(Edtied: Got everything working as expected except for one problem. I wasn't completely honest when I said that the detailsScreen(activityB in your example) was the last in the hierarchy. On this screen the user actually has the option to view a map (有趣的是:除了一个问题,一切都按预期方式工作。当我说detailScreen(示例中的activityB)是层次结构中的最后一个时,我并不完全诚实。在此屏幕上,用户实际上可以选择查看地图

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
                Uri.parse("geo:0,0?q=" + depLatitude + "," + depLongitude + "(" + name + ")"));
                startActivity(intent);

When I press back on the map, the ideal situation would be for it to return to the detailsScreen, but in this case it goes right back to the mainScreen. 当我在地图上按回时,理想的情况是使其返回到detailsS​​creen,但在这种情况下,它又回到了主屏幕。

Any ideas? 有任何想法吗? )

You can use Reorder to front attribute and get the same instance of mainScreen to the top. 您可以使用“重新排序”将属性置于最前面,并使mainScreen的相同实例到达顶部。 For destroying activities which you dont want as soon as they go in the background use noHistory attribute 对于销毁您不希望在后台运行的活动,请使用noHistory属性

MainActivity → Activity A → Activity B → Activity A

Launch Activity A from MainActivity 从MainActivity启动活动A

Intent intentA = new Intent(MainActivity.this, ActivityA.class);
startActivity(intentA);

In this code snippet we are launching Activity A in the standard mode. 在此代码段中,我们以标准模式启动活动A。

Launch Activity B from Activity A 从活动A启动活动B

Activity B is launched with Intent flag : Intent.FLAG_ACTIVITY_REORDER_TO_FRONT from Activity A 活动B是通过活动A的Intent标志启动的:Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

Intent intentB = new Intent(ActivityA.this, ActivityB.class);
intentB.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intentB);

Launch Activity A from Activity B 从活动B启动活动A

When the Activity A is launched from Activity B the previous instance of the activity gets reordered to the front instead of creating a new one 当从活动B启动活动A时,活动的先前实例将重新排序到最前面,而不是创建一个新实例

Intent intentA = new Intent(MainActivity.this, ActivityA.class);
startActivity(intentA);

No History 没有历史

<activity android:noHistory="true" ../>

Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. 当用户离开活动并在屏幕上不再可见时,是否应该从活动堆栈中删除该活动并完成(调用了finish()方法)-如果应结束,则为“ true”,否则为“ false”如果不。 The default value is "false". 默认值为“ false”。 A value of "true" means that the activity will not leave a historical trace. 值为“ true”表示该活动将不会留下历史痕迹。 It will not remain in the activity stack for the task, so the user will not be able to return to it. 它不会保留在任务的活动堆栈中,因此用户将无法返回到该任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android分享动作。 从导航抽屉而不是顶部栏触发。 - Android share action. Trigger from navigation drawer rather than top bar. Android:如何从通知中恢复应用程序? - Android: How to resume an App from a Notification? 如何从通知栏启动Phonegap Android应用程序 - how to start Phonegap Android application from notification bar 从android中的通知栏启动一个应用程序 - start an application from notification bar in android 从通知栏启动Android应用程序:安装后 - Launching an Android app from notification bar : Just after installation Android如何在不打开应用程序的情况下从通知栏恢复和暂停音频? - Android how to resume and pause audio from notification bar without open app? Android状态栏通知虽然已经运行,但会启动新应用。 如何从应用程序图标和状态栏通知同步启动应用程序? - Android statusbar notification launches new app though it running already. How to synchronize launching app from app icon and staus bar notification? 从通知栏恢复我的Android应用程序(应用程序图标仍显示在栏中) - Resume my android app from Notification bar (and app icon still shows in bar) 如何使通知意图恢复而不是制定新的意图? - How to make notification intent resume rather than making a new intent? Android:在关闭通知栏时,视图会在状态栏下调整大小。 如何禁用调整大小? - Android : On Notification bar dismiss, view gets resized under status bar. How to disable the resizing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM