adjfac adjfac - 1 month ago 9
Android Question

Can I chain async task sequentially (starting one after the previous asynctask completes)

Every time I do a

httpRequest
, the screen will appear to be locked up for a few seconds while the code is executing. Hence I used
AsyncTask
to do all my
httpRequest
stuff in a separate thread while putting up a
ProgressDialog
so the user knows something is happening.

I recently encountered the following situation: the input of one of my
httpRequest
is dependent on the result from a previous
httpRequest
(+parse) action. I can't just put the two
AsyncTask
s sequentially cause Android will put them in two threads and start the second one without the first one being finished. And without an appropriate input (the result of the first
httpRequest
), my second
httpRequest
will crash the app.

Is there way I can put-in a
wait()
to force the second
AsyncTask
not to start until the first one finishes?

Answer

I also had some same situation the other day. I had solved it in this way: Pass the reference of your activity to the constructor of async class and execute the do in background function. Now in post execute function call a public method of ur activity from async class to execute the task again... or try this:

            if (asynclass.getStatus() == android.os.AsyncTask.Status.PENDING) {
                asynclass.execute();
            } else if (RF.getStatus() == android.os.AsyncTask.Status.FINISHED) {
                asynclass = new asyncclass();
                asynclass.execute();
            } else {
                Toast.maketoast(this, "Plz wait", 1).show();
            }

Cheers