Can Honeycomb Loaders solve problems with AsyncTask + UI update?

你说的曾经没有我的故事 提交于 2019-12-03 06:01:23

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5097565/can-honeycomb-loaders-solve-problems-with-asynctask-ui-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!