Android AsyncTask inside AsyncTask

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 ?

  • How to restore fragment back stack with in an activity (After application is killed in backgroud)
  • Error : Unable to execute dex
  • Android: Your content must have a ListView whose id attribute is android.R.id.list
  • How to add google-play-services.jar project dependency so my project will run and present map
  • how to implement a long click listener on a listview
  • Why ButterKnife uses a DebouncingOnClickListener for @OnClick methods?
  • (english isn’t my primary language, I hope I made myself clear).

    Related posts:

    Multiple Apps use same content provider
    How to prevent android app from updating from the google play store?
    OpenGL surface view scaling for different DPIs
    programmatically hide/disable emoticons on Android soft keyboard
    How to keep a bottom nav bar from being pushed up on keyboard shown
    Android - Showing Phonebook contacts and selecting one
  • android Call Forwarding programmatically
  • Android webview cannot render youtube video embedded via iframe
  • How to destroy old fragments in FragmentStatePagerAdapter
  • setMinDate(…) for DatePicker doesn't work when invoked a second time
  • Android Error - Open Failed ENOENT
  • Listview not clickable when the textview inside that listview is selectable
  • 3 Solutions collect form web for “Android AsyncTask inside AsyncTask”

    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.

    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

    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/

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.