可以蜂窝装载机解决问题的AsyncTask + UI更新?解决问题、蜂窝、装载机、UI

2023-09-03 21:29:09 作者:天上无云不下雨

做一些背景,然后更新UI是很难在Android的正确执行。它只是糟糕的设计。典型的例子就是取东西从网络和显示结果的AsyncTask的。有2个问题是:

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:

的AsyncTask的具有参考到活动(因为它需要更新其UI)。屏幕方向更改后,活动将重新启动。但AsyncTask的还是引用旧破坏活动,因此它无法更新新的活动的用户界面。

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.

这会导致OutOfMemoryException异常。试想一下,你有很多位图的活动,并启动一些AsyncTask的。您preSS BACK(活动结束),但AsyncTask的仍在运行,因为它引用的活动,与位图的活动仍然在内存中。重复上述步骤(Start活动和背面),你有一个强制关闭迟早的事。

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.

这是可以解决的,但它是太复杂了。在一个活动我有3个不同的AsyncTasks,他们每个人可以同时运行多个实例。正确地实施,这是令人沮丧的。在code就真的很难理解和调试。

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.

可以蜂窝装载机的某种方式解决这个问题?而且是有办法使用它们,pre-蜂巢的Andr​​oid版本?

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

推荐答案

是的,从我的经验与装载机他们似乎解决了人们的普遍问题 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.

 
精彩推荐
图片推荐