Can Honeycomb Loaders solve problems with AsyncTask + UI update?
Asked Answered
S

2

13

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?

Salty answered 23/2, 2011 at 21:44 Comment(0)
B
7

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.

Bargeboard answered 23/2, 2011 at 22:25 Comment(0)
N
4

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.

Nannette answered 23/2, 2011 at 21:53 Comment(7)
Thanks, but in the example, only problem 1. is solved - the activity field in the AsyncTask should be nulled in onDestroy(). I use my own subclasses of AsyncTask and Activity to help me solve problems 1. and 2. but it is still too complicated. Doing asynchronous work should be easy.Salty
The problem 2 is also solved: every time a new Activity is created a reference inside AsyncTask is updated, so that it does point to this new Activity. So nothing is referencing the old Activity and it can be GCed.Furfuraceous
@Mike: thanks for a great link! I'll use this in my future projects.Furfuraceous
@Peter Knego: Unfortunately it isn't... Let's say I press BACK, the Activity is finished and AsyncTask still running. Then I start the Activity again (e.g. from home screen) and new instance of AsyncTask is created - 2 AsyncTasks are now running, each pointing to a different instance of Activity. getLastNonConfigurationInstance() works only with Activity restarts due to configuration change.Salty
@funcho: I will work as long as the process is not killed (which also kills AsyncTask). The code uses onRetainNonConfigurationInstance() which was created to carry Objects between Activity instances. This thread talks about this #4286377Furfuraceous
@funcho: You are right, I just tested it. Back button and restart through home (or task list), does not carry saved Objects from activity to new activity.Furfuraceous
@Peter Knego: Another solution (which I use) is, that in onDestroy() I null the AsyncTask's reference to Activity. In the AsyncTask I can't use the activity field in doInBackground(). In onPostExecute() and onProgressUpdate() I must check whether activity == null, if so, I just return.Salty

© 2022 - 2024 — McMap. All rights reserved.