Honeycomb Loaders 可以解决 AsyncTask 的问题吗?用户界面更新?

发布于 2024-10-18 14:40:33 字数 675 浏览 8 评论 0原文

在后台执行某些操作然后更新 UI 在 Android 中很难正确实现。它的设计很糟糕。典型的例子是一个 AsyncTask,它从网络上获取一些东西并显示结果。这有两个问题:

  1. AsyncTask 引用了 Activity(因为它需要更新其 UI)。屏幕方向更改后,活动将重新启动。但 AsyncTask 仍然引用旧的已销毁 Activity,因此它无法更新新 Activity 的 UI。

  2. 这可能会导致 OutOfMemoryException。想象一下,您有一个包含大量位图的活动并启动一些 AsyncTask。您按“返回”(活动已完成),但 AsyncTask 仍在运行,并且由于它引用该活动,因此带有位图的活动仍在内存中。重复此操作(启动 Activity 并返回),迟早会强制关闭。

这个问题是可以解决的,但是太复杂了。在一个 Activity 中,我有 3 个不同的 AsyncTasks,每个任务都可以同时在多个实例中运行。正确地实施这一点是令人沮丧的。代码变得非常难以理解和调试。

Honeycomb Loaders 可以以某种方式解决这个问题吗?有没有办法在 Honeycomb 之前的 Android 版本中使用它们?

Doing something in background and then updating UI is very hard to implement correctly in Android. It's simply badly designed. Typical example is an AsyncTask that fetches something from the web and displays the result. There are 2 problems with this:

  1. The AsyncTask has a reference to Activity (because it needs to update its UI). After screen orientation change, the Activity is restarted. But the AsyncTask still references to the old destroyed Activity therefore it can't update the UI of the new Activity.

  2. This can lead to OutOfMemoryException. Imagine that you have an Activity with lots of bitmaps and start some AsyncTask. You press BACK (Activity is finished) but the AsyncTask is still running and because it references to the Activity, the Activity with bitmaps is still in memory. Repeat this (start Activity and BACK) and you have a force close sooner or later.

This can be solved, but it is way too complicated. In one Activity I have 3 different AsyncTasks, each of them can be running in several instances simultaneously. Implementing this correctly is frustrating. The code becomes really hard to understand and debug.

Can Honeycomb Loaders somehow solve this? And is there a way to use them in pre-Honeycomb Android versions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-10-25 14:40:33

是的,根据我使用 Loader 的经验,它们似乎解决了人们在 AsyncTasks 和配置更改方面遇到的常见问题。

我认为谷歌说片段静态库也将包括加载器,所以是的,当库发布时它们也应该向后工作。

Yes, from my experience with Loader they seem to solve the common problems people have with AsyncTasks and configuration changes.

I think Google said that the fragments static library would include Loaders as well so yes, they should work backwards too when the library is released.

四叶草在未来唯美盛开 2024-10-25 14:40:33

这并不能回答您有关 Honeycomb 加载程序的问题,但以下链接具有可以轻松处理 AsyncTasks 方向更改的模式。

http://evancharlton.com/thoughts/rotating-async-tasks/

有一些那里还有其他很棒的帖子。

更新:正如OP在评论中指出的那样,这只适用于配置(方向)更改,但在使用“后退”按钮并通过主菜单或任务列表重新启动时不起作用。

如果您一次需要一个 AsyncTask,那么您可以在 Activity 内使用对 AsyncTask 的静态引用。另一种选择是保存对应用程序的引用。

然后,当启动新的 Activity 时,您会查看是否有 AsyncTask 正在运行,并将其自身设置为当前 Activity(通过 AsyncTask 上的 setter)。确保同步访问 AsyncTask 内的 Activity。

This does not answer your question about Honeycomb loaders but the following link has a pattern that easily handles orientation changes for AsyncTasks.

http://evancharlton.com/thoughts/rotating-async-tasks/

Theres some other great posts on there as well.

Update: As OP noted in comments this only works for configuration (orientation) changes, but does not work when using BACK button and restart via Home menu or tasks list.

If you need one AsyncTask at a time, than you could use a static reference to AsyncTask inside Activity. The other option would be to save the reference to Application.

Then when a new Activity is started, you look if there is an AsyncTask running and set itself as current Activity (via a setter on AsyncTask). Be sure to synchronize access to Activity inside AsyncTask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文