片段应该如何获得有关异步任务结果的通知?
我有一个使用片段的活动。这些片段可能会根据用户的交互来来去去。其中许多片段将作业启动到 IntentService,从而以这种方式异步运行。 IntentService 应如何报告这些作业的结果?
启动作业的片段可能不存在。如果作业完成并且起始片段当前处于活动状态,那么它应该收到有关此情况的通知,并采取相应的行动。如果不是,则无需执行任何操作。
我考虑过使用广播意图和 BroadcastReceiver 组件,但片段无法注册接收器,只能注册活动。
您会建议什么解决方案?
I have an Activity that uses fragments. These fragments may come and go, based on the users interactions. Many of these fragments launch jobs to an IntentService, which get to run async this way. How should the IntentService report back the results of these jobs?
The fragment that started the job may of may not be present. If a job finishes and the starting fragment is currently active, then it should get notified about this, and act accordingly. If it's not, then no action is needed.
I've thought about using broadcast intents and BroadcastReceiver components, but fragments can't register receivers, only activities.
What solution would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 IOSched 应用(适用于 Android 的 Google I/O 应用)中注意到同样的问题。
他们创建了 DetachableResultReceiver,其中扩展 SDK 类 ResultReciever。
他们很容易 在片段中使用它。
接收者:
活动和片段:
I noticed the same problem in IOSched App (Google I/O App for Android).
They created DetachableResultReceiver, which extends SDK class ResultReciever.
And they easily use it in Fragments.
Receiver:
Activity and Fragment: