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?
AsyncTask instances can only be used one 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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With