在另一个活动中启动asynctask后刷新当前视图(Refresh current view after asynctask started in another activity)

编程入门 行业动态 更新时间:2024-10-20 21:02:03
在另一个活动中启动asynctask后刷新当前视图(Refresh current view after asynctask started in another activity)

我有一种情况,我有多个活动处理单个全局对象列表。

从活动A开始,我可以启动一个asynctask,它发送一个HTTP Get请求来检索XML消息,然后将XML文件解析为一系列对象。

我的目标是只有在底层数据结构发生变化时才能刷新所有视图。

理想情况下,这应该意味着我可以启动活动A,调用asynctask,移动到活动B,CD等,并且当asynctask完成时(无论我在哪个活动上),都应该更新当前视图。

我已经调查了广播听众,但我不确定我是否正确的方向,有人可以解释这是否可能?

谢谢

I have a situation where I have multiple activities that deal with a single global list of objects.

From activity A, I can start an asynctask which sends an HTTP Get request to retrieve an XML message then parses the XML file into a series of objects.

My aim is to be able to refresh all views, only if the underlying data structure changes.

This should ideally mean that I can launch activity A, call an asynctask, move onto activity B, C D etc, and when the asynctask completes (no matter what activity I am on) the current view should be updated.

I've looked into broadcast listeners but I am not sure I am on the right direction with this, would it be possible for somebody to explain if this is possible?

Thanks

最满意答案

由于AsyncTask实例依赖于调用者Activity ,我认为这不是正确的方法。 在您的情况下,由于您需要在任何 Activity恢复结果,我将使用未绑定的Service以便随时随地访问它。

您还可以使用本地BroadcastReceiver ,它将向您的所有Activity发送信号,并且那些注册发送AsyncTask / Service的操作的人将执行您可能需要的必要操作。 请注意,好像您的Activity尚未启动,显然Activity不能处理您发送的内容,因为该接收者应该取消注册。

Since AsyncTask instances are dependent of the caller Activity, I think this is not the correct approach. In your case, as you need to recover the result within any Activity, I'd use an unbound Service so it might be accessed whenever you want, wherever you want.

You might also use a local BroadcastReceiver which would send a signal to all of your Activitys and, those registering the action that sent your AsyncTask/Service, would perform the necessary actions you may need. Be careful as if your Activity has not been started, obviously that Activity won't be able to process what you've sent since that receiver should be unregistered.

更多推荐

本文发布于:2023-08-05 20:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   活动中   asynctask   Refresh   activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!