0

I have multiple asynctasks in my application, I want to execute it when a specific condition is satisfied. For example,

class Task1 extends AsyncTask...

class Task2 extends AsyncTask..

class Task3 extends AsyncTask...

I have a while loop which is used to iterate some values from a map. eg:

while(iterator.next())

{

 Map.Entry pair = (Map.Entry) it.next();

            String title = pair.getKey().toString();

if(title.equals("one"))

{

//execute task1;

}else if(title.equals("two"))

{

//execute task2;

}

etc..
}

This is a sample code i currently used. Each Task contains a progress dialog, which is starting on onPreExcecute() and dismissed on onPostExecute().

My Problem is that I want to execute only one task at a time. In my code all tasks are started at same time and progress dialog will overlap each other. So there is any way to execute only one task at a time, Thanks is advance.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
userDroid
  • 198
  • 9

3 Answers3

1

The problem here is while loop. The loop will continue to run even if a task is running in background. So you must modify your code such a way that the loop condition is triggered only if one task is completed.

To handle the progressbar issue, create a global progressbar object which can be shared by all the asyncTasks.

Refer the following link for checking status of AsynTasks. AsynTask Status

Community
  • 1
  • 1
Balu SKT
  • 549
  • 5
  • 22
0

The main reason that overlap the progressDialog is running the multipple asnyntask . so you start first asyntask and the call next form its postExcecutive.

sijo jose
  • 130
  • 9
0

You have misunderstanding of AsyncTask. The default executor of AsyncTask is a serial executor, which means that it will executes tasks one at a time in serial order in a particular process. But why the process dialogs are shown at a time? After you read the code of AsyncTask, you can find the reason in function executeOnExecutor:

public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
            Params... params) {
    mStatus = Status.RUNNING;
    onPreExecute();
    mWorker.mParams = params;
    exec.execute(mFuture);
    return this;
}

You can find that method onPreExecute is called before execute, ie it is not serial called. So the progress dialogs are appearing at the same time.

I think you can modify your code by using only one progress dialog in your main activity and show or hide the dialog by its status.

GGCoke
  • 566
  • 4
  • 4