5

I'm writing an app that does the following:

  1. Parses a webpage and extracts image URLs from it
  2. Decodes them to Bitmap and shows them in an ImageView

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 onPostExecute() method.

Is this considered bad practice? If so, how should i do this?

(this is a theoretical question, i'm not asking for code)

  • its not bad practice. You can chain asynctask but in your case you have to make sure they happen in order. see this answer http://stackoverflow.com/questions/13228096/android-asynctask-order-of-execution – Sam May 13 '15 at 07:26

5 Answers5

3

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 AsyncTasks possible.

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.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
1

Can i chain AsyncTasks to achieve this? I mean starting the second one from the first one's onPostExecute() method

The only constraint is that the AsyncTask has to be instantiated on the UI Thread. As long as you instantiate it on the UI Thread you should be safe. So the answer is yes, you can do it.

If so, how should i do this?

There are different approaches to solve the same problem. You could use an ExecutorService for instance, and a Delegate/Listener, an Observer or a BroadcastReceiver to notify the completeness of a task

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Of course, you can. Theoretically, this logic you talked about above can't be wrong. But, you have to understand that: onPreExecute and onPostExecute are called on the Main Thread, namely the UI Thread while doInBackground on another non-main-thread. Meanwhile, if you executed the second AsyncTask on the doInBackground of the 1st one, onPreExecute and onPostExecute of the 2nd one wound not be run on the Main Thread. Take notice of that.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
1

There's a good answer for this here but since you're asking theoretically, I'll tag an opinion onto the end of it.

From the AsyncTask docs:

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

Chaining of Futures (or Promises) is a perfectly normal asynchronous practice. Future A does something and returns a value; this value can then be consumed by another asynchronous chunk of code, and so on, forming a logical processing chain.

In pseudo-code, it looks like this:

Future( calculate and return x).map( consume x and return y)
                               .map ( consume y and so on)
Community
  • 1
  • 1
Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
0

Well, I don't think it is bad practice. The only downside I can see is that code becomes a little more difficult to read, especially if you end up chaining a lot of callbacks.

francesco foresti
  • 2,004
  • 20
  • 24