3

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.

Krishna Prasad
  • 693
  • 1
  • 6
  • 19

3 Answers3

4

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.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • thank u rajesh, I will get success with u'r logic, I create one global variable in activity and every time I increase the value in postexecute() and check when the value reach to "3" I dismiss the dialog. – Krishna Prasad May 11 '12 at 06:57
1

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.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
0

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();
waqaslam
  • 67,549
  • 16
  • 165
  • 178