Android AsyncTask inside AsyncTask

So, I'm working on a barcode decoder, which, after we get the barcode, multiply the API via the Internet to decrypt what was just scanned. The thing is, I need to bind some XML parsing together, and I don't know if I am doing this correctly.

So, after scanning the barcode, my program calls ASyncTask, which passes the API to get the product name. After it has a name, I want it to call another ASyncTask. I know that this is possible by creating an ASyncTaks instance in onPostExecute() another, but I think this is wrong, because it looks like boxes inside boxes. So is it not possible / better to instantiate a second ASyncTask inside my main operation and make it wait until my first ASyncTask is complete?

(English is not my main language, I hope I have made it clear).

+6
source share
3 answers

I think it is absolutely legal to start the second AsyncTask in onPostExecute first AsyncTask, Mixing both operations is a bad logical idea, As "The Offspring" said - "You have to keep" divided "

If you do not want it to be directly inside onPostExecute itself, set a handler to execute it in action and call this handler from onPostExecute .

And the last: if you have a lot of logic - move it to a separate file, do not save it in one file.

+17
source

In such situations, it may be better to perform lengthy operations together in the same AsyncTask.

Another option is to use the Loaders API, which greatly simplifies the tasks of the http://developer.android.com/guide/topics/fundamentals/loaders.html chain

+1
source

You can take a different approach if you are faced with such a situation. That is, to combine requests and operations inside runnables / callables and manage them separately, for example, in a queue. Here is a good approach. http://ugiagonzalez.com/2012/07/02/theres-life-after-asynctasks-in-android/

0
source

Source: https://habr.com/ru/post/915190/


All Articles