This question already has an answer here:
I'm writing an app that does the following:
- Parses a webpage and extracts image URLs from it
- Decodes them to
Bitmap and shows them in an
I don't want these to run on the UI thread and obviously the 2nd point can't be performed without the 1st being completed.
Can i chain AsyncTasks to achieve this? I mean starting the second one from the first one's
Is this considered bad practice? If so, how should i do this?
(this is a theoretical question, i'm not asking for code)
Best How To :
You can definitely do that and there is nothing wrong with chaining multiple
AsyncTasks if you really have to. And I want to emphasise: only if you really have to.
AsyncTasks bring a certain overhead with them. If you chain two
AsyncTasks you get twice the overhead. You don't want that. You should use the least amount of
In your case the solution actually seems quite simple: Perform all of the work in just one
AsyncTask. Ask yourself: Is there really any need for two separate
AsyncTasks? From your description it certainly doesn't seem like it. If you just use one
AsyncTask your code will run a lot more efficiently than if you use two.
Please note that
AsyncTasks should only be used for short operations that take a few seconds and which are started in response to a user action. If you need to perform some long running operation in the background than an
AsyncTask is probably not the best solution. I can't really tell from your description if what you want to do is something suited for an
AsyncTask or not, but if the work you are doing doesn't fall into the criteria I described above you are probably better of with an alternative solution.