thatmarcel
thatmarcel

Reputation: 398

How to start a new Activity after several async tasks had finished

I have a big Problem:

I want to start a new Activity after several Async-Tasks had finished. My problem is that the Async-Tasks are dynamically started. Some time I have only one Async-Task, some time I have five Async-Tasks. And the even bigger problem is, that all are the same Async-Tasks, only with another URL. Because they're Download-Tasks.

Could anyone help?

Solution:

I created a counter that every available Update counts 1 higher. So if five Updates are available, the counter will be set to 5. Each finished Update and Unzip, the counter will be set 1 lower. If counter == 0 it would open the new Activity.

Thanks for all of your Answers.

Upvotes: 0

Views: 88

Answers (3)

Beloo
Beloo

Reputation: 9925

I guess running every AsyncTask on an Executor and then waiting for result should fullfill your needs. To become familiar with executor this topic may help. By the way, in case right implementation, you've already should run that tasks on it to avoid time consuming task pools. Async task can been executed on executor via AsyncTask.executeOnExecutor(Executor exec, Params... params) method.
After that create managing thread (i strong recommend doing that with IntentService and move all tasks-managing logic there) , which will wait for tasks completion and send event to start activity. How to wait that all tasks in executor have completed? Well, read this question. There provided more detailed answers, than i can provide.
I'm quite sure that i've described you the best approach.

Upvotes: 0

Rajesh Gopu
Rajesh Gopu

Reputation: 893

Use a global variable counter assigned with number of AsynTasks,
On every onPostExecute decrement the counter

ON if(counter ==0) Start your new Activity

class A{
int counter =0;

public doJob(int jobCOunt){
    this.counter = jobCount
    new Job().execute();
}
class Job extends AsyncTask{
...
  protected void onPostExecute(Boolean success) {
    counter--;
    if(counter == 0){
        startActivity
    }
  }

}

}  

Upvotes: 1

Rahul
Rahul

Reputation: 10635

Just curious to know can't we move loop of different URLs in doInBackground() for getting results? I think this can resolve your big problem and will also easy to manage single AsyncTask.

Upvotes: 0

Related Questions