Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 AsyncTasks 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?

like image 330
adjfac Avatar asked Sep 21 '11 04:09

adjfac


People also ask

How many times an instance of async task can be executed?

AsyncTask instances can only be used one time.

Can we run multiple async task at a time?

executeOnExecutor(AsyncTask. THREAD_POOL_EXECUTOR); So in order to execute multiple AsyncTask in parallel, we have to use THREAD_POOL_EXECUTOR and execute the AsyncTasks.

What happens to AsyncTask if activity is destroyed?

If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes.

Does Async run in parallel?

The method async. parallel() is used to run multiple asynchronous operations in parallel. The first argument to async. parallel() is a collection of the asynchronous functions to run (an array, object or other iterable).


1 Answers

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

like image 122
Awais Tariq Avatar answered Oct 04 '22 00:10

Awais Tariq