0

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.

thatmarcel
  • 388
  • 3
  • 15
  • Please let me know what are the parameters on which you decide number of asyncs. – Rahul Oct 15 '16 at 17:06
  • The app compares different values from a server with the values on the device. If the values on the server are higher, Files from the server will be downloaded and unzipped. – thatmarcel Oct 15 '16 at 17:26
  • I think we can manage this logic in single AsyncTask. – Rahul Oct 15 '16 at 17:27
  • modify your AsyncTask to work on a list of URLs (vs just one). sometimes you give it a single url, sometimes five. it really doesn't matter. when the task completes either the entire list has been downloaded (and you can start the next activity), or one or more files failed (and you can show an error msg). – trooper Oct 15 '16 at 17:33
  • So there are about 15 different folders on the server and it can be that only 1 or 5 or 13 must be updated. And i dont know before which. – thatmarcel Oct 15 '16 at 17:33
  • So do that parallelly in doInbackground() of single AsyncTask. – Rahul Oct 15 '16 at 17:50

3 Answers3

1

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
    }
  }

}

}  
Rajesh Gopu
  • 863
  • 9
  • 33
0

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.

Rahul
  • 10,457
  • 4
  • 35
  • 55
0

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.

Community
  • 1
  • 1
Beloo
  • 9,723
  • 7
  • 40
  • 71