Krishna Prasad
Krishna Prasad

Reputation: 691

Handle single progressdialog with parallel multiple asynctask

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

Answers (3)

Mark Pazon
Mark Pazon

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

Rajesh
Rajesh

Reputation: 15774

There are different ways to handle this situation.

  1. 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.

  2. You may not need the 3 different AsyncTasks. You can use a single AsyncTask with a CountDownLatch for Thread synchronization.

Upvotes: 4

waqaslam
waqaslam

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

Related Questions