Reputation: 691
In my Project I want to parallelly load the data from 3 different url's(webservice) while loading the page.
for this I use 3 different asynctask to load data.
I want to show the progress dialog message as "loading" when first asynctask is started and close dialog after last asynctask is completed(until complete 3 tasks show progress dialog).
Please tell me the way to handle this situation.
Upvotes: 3
Views: 3744
Reputation: 6205
One way you can do is you can create an interface OnThreadFinishListener
(with an onThreadFinish()
method) and have your activity implement it. All of your AsyncTask
should register itself to the listener so each time a thread finishes it calls the onThreadFinish
method. in the onThreadFinish method it should check if there is still a thread running in the background (something like if (asyncTask1.getStatus() == AsyncTask.Status.RUNNING || asyncTask2.getStatus() == AsyncTask.Status.RUNNING ... )
)
If all are false then you can dismiss the progress dialog
.
Upvotes: 1
Reputation: 15774
There are different ways to handle this situation.
You can use a counter variable initialized with the number of tasks, and this counter gets decremented when an AsyncTask
is complete. When the counter is 0, the ProgressDialog
is dismissed. You can do this decrement and checking for progress dialog dismissal in each of the AysncTask
's onPostExecute
.
You may not need the 3 different AsyncTask
s. You can use a single AsyncTask
with a CountDownLatch
for Thread
synchronization.
Upvotes: 4
Reputation: 68177
You may do it as below:
//define progress dialog object globally in your parent class
ProgressDialog pd = null;
AsyncTask1 onPreExecute
:
pd = ProgressDialog.show(this, "title", "loading", true);
AsyncTask2:
//do nothing
AsyncTask3 onPostExecute
:
if(pd!=null)
pd.dismiss();
Upvotes: 0