6

So, I'm working on a barcode decoder, which once we have the barcode goes to multiples API over the internet to decode what was just scanned. The thing is that I have to link some XML parsing together, and I don't know if I'm doing it right.

So, once the barcode is scanned, my program calls an ASyncTask which goes over an API to retrieve the product name. Once it has the name, I want it to call another ASyncTask. I know this is possible by instantiating an ASyncTaks in the onPostExecute() of the other but, I think this is wrong, because it's like boxes within boxes. So isn't it possible/better to instantiate my second ASyncTask inside my main Activity, and make it wait until my first ASyncTask is finished ?

(english isn't my primary language, I hope I made myself clear).

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
MagicMicky
  • 3,819
  • 2
  • 37
  • 53
  • 3
    Whats the reason to use 2 tasks? why not do what you want in the first tasks doInBackground? – Renard May 08 '12 at 11:50
  • 3
    We shouldn't make fun of one's language (english) as it might not be his/her primary language – waqaslam May 08 '12 at 11:53
  • @Renard it seems too much messy to let everything in the doInBackground. My first ASyncTask can return different results which first needs to be treated, and then, according to the result, do some more parsing. So launching other ASyncTask allow me to divide the code in multiple parties, and make it more clear. – MagicMicky May 08 '12 at 12:18
  • 1
    @MagicMicky: yes, but you don't necessarily need an asynctask for that. You may just need a different method. – njzk2 Dec 16 '14 at 20:17

3 Answers3

17

I think it's absolutely legitimate to start the second AsyncTask in the onPostExecute of the first AsyncTask, Mixing both operations is a bad logical idea, As "The Offspring" said - "You've gotta keep 'em separated"

If you don't want it to be directly inside the onPostExecute itself, set a handler to execute it in the activity and call this handler from onPostExecute.

And last thing - If you have a lot of logic - move it to a separate file, don't keep it all at the same file.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • Okay, I'm going to try to set a handler in my activity, and calling it from the `onPostExecute()`. And yeah, I got multiple files, even multiple packages. This is my first real Android project, I want it to be as clean as possible ! – MagicMicky May 08 '12 at 12:10
1

In situations like this it might be better to batch long running operations together in one AsyncTask.

Another option is to use the Loaders API, this makes chaining tasks much easier http://developer.android.com/guide/topics/fundamentals/loaders.html

Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
0

You can go for another approach if you are facing often a situation like this. That is to merge requests and operations inside of runnables/callables and to manage them separately within say a queue for instance. Here is a nice approach. http://ugiagonzalez.com/2012/07/02/theres-life-after-asynctasks-in-android/

Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26