Android的,建议有什么意图标志被通知开始活动 [英] Android, what Intent flags are recommended for Activities started by Notifications

查看:172
本文介绍了Android的,建议有什么意图标志被通知开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个特殊的问题,一个应用程序有多个活动。我有一个绑定到服务的屏幕管理器类。该服务轮询服务器的数据。屏幕管理器启动活动A,B,或C,基于数据。它也将允许用户选择要显示的其他活动,或者它可以换出一个活动自动根据从服务的新数据。目前,所有导航的伟大工程,如果用户presses家,适用活动回来到前台当用户presses从Android主屏幕或最近运行的应用程序列表中的应用程序图标。

然后我不得不实施一项新功能,以显示图标和通知。我第一次只显示该通知时的活动不再由每个活动的onPause设置它可见的实现这一点。工作就像一个魅力,给用户一个第三个选项,以重新显示后,Home键preSS的应用程序。但是,如果从服务器(当不显示该应用)的数据导致屏幕管理器更新活动要显示我有问题。我觉得我的活动堆栈是越来越搞砸了。因为我已经尝试了一点不同的模型,其中屏幕管理器处理的通知,并始终显示通知,以某项活动被更新,但它仍然没有削减它一个新的意图进行更新。

当应用程序被最小化,数据的变化,我可以看到屏幕的经理打电话startActivityForResult的新的活动,它看起来像Android的知道,我们最小化,并不会显示在活动。我然后也屏幕Manager呼叫完成对老顶。

每个活动目前的国旗在code设置为singleTop的清单和FLAG_ACTIVITY_SINGLE_TOP。当数据在服务器上没有改变,我拉的应用程序备份使用上述任何一项我得到了我期望的,活动的onNewIntent被调用。然而,当数据发生了变化,用户调用了通过它调用活动的onCreate通知的应用程序。该活动不上来并运行,但随后pressing回来带我到什么似乎像一样的活动,而不是退出的另一个实例。我只是在仔细看看我的logcat,我看到后2调用活动的onResume,但后来没有双打。

我觉得也许我只是缺少对通知的意图简单的东西像另一个意图的标志?感谢您的任何想法!

解决方案
  

什么是从服务器时,没有显示改变杀了我的数据。

我不知道这意味着什么。

  

我觉得也许我只是缺少对通知的意图简单的东西像另一个意图的标志?

我用 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP 来对付把我带回到什么似乎像同一活动的另外一个实例的问题,但对我来说,我有一个单一的活动应用程序。

的文档 FLAG_ACTIVITY_CLEAR_TOP 也说:

这种发射方式也可以用来配合良好的效果与FLAG_ACTIVITY_NEW_TASK:如果用来启动任务的根系活力,就会带来这个任务的任何当前正在运行的实例推到前台,然后将其清除其根的状态。这是特别有用的,例如,发射时从通知管理器的活动。

还有 FLAG_ACTIVITY_REORDER_TO_FRONT

例如,考虑由四个活动任务:A,B,C,D。若D与解析为活动B的成分的意图调用startActivity(),那么B就会被带到前面历史堆栈,与此产生的顺序:A,C,D,B。此标志将在同时指定FLAG_ACTIVITY_CLEAR_TOP忽略了

也许这些模式来选择一个适合您的需要。

I am running into a peculiar issue with an app which has multiple Activities. I have a screen manager class that is bound to a service. The service polls a server for data. The screen manager starts Activity A, B, or C, based on the data. It will also allow the user to select to display the other Activities or it may swap out an Activity automatically based on new data from the service. Currently all the navigation works great and if the user presses Home, the applicable Activity comes back to the foreground when the user presses the apps icon from the Android home screen or the recently run apps list.

I then had to implement a new feature to display an icon and notification. I first implemented this by only displaying the notification when the Activities were no longer visible by setting it in each Activities onPause. Worked like a charm and gave the user a third option to redisplay the app after Home button press. However if the data from the server (when the app is not displayed) causes the screen manager to update the Activity to be displayed I have issues. I think my Activity stack is getting screwed up. I have since tried a little different model where the screen Manager handles the notifications and always displays the notification, updating it with a new Intent whenever an Activity is updated, but its still not cutting it.

When the app is minimized and the data changes I can see the screen manager call the startActivityForResult on the new Activity and it seems like Android knows we are minimized and does not display the Activity. I can then also the screen Manager call to finish on the old top Activity.

Each Activity currently has the flag set to singleTop in the manifest and FLAG_ACTIVITY_SINGLE_TOP in the code. When the data hasn't changed on the server, I pull the app back up using any of the above I get what I expect and the Activity's onNewIntent is called. However when the data has changed and the user calls up the app via the notification it calls the Activities onCreate. The Activity does come up and runs, but then pressing back takes me to what seems like another instance of the same Activity instead of exiting. I just took a closer look at my logcat and I see 2 calls to the Activity's onResume, but then no doubles after that.

I feel like maybe I'm just missing something simple like another intent flag on the notification's Intent? Thanks for any ideas!

解决方案

What is killing me is the data from the server changing when not displayed.

I have no idea what this means.

I feel like maybe I'm just missing something simple like another intent flag on the notification's Intent?

I used Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP to deal with the "takes me back to what seems like another instance of the same Activity" problem, but in my case, I had a single-activity application.

The documentation for FLAG_ACTIVITY_CLEAR_TOP also says:

"This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager."

There is also FLAG_ACTIVITY_REORDER_TO_FRONT:

"For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified."

Perhaps one of those patterns fits your need.

这篇关于Android的,建议有什么意图标志被通知开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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