1

I am using multiple spinner classes in my android app like country,state,city.

On change of country spinner, state has to be loaded and on change of state ,city has to be loaded

.Since I am making REST call to load the combos, I need to go with AsyncTask tasks.

How to call asysnc tasks one after other since I need to pass some value from Async Task1 to Async Task 2.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_location); 
        runMultipleAsyncTask();


    }
    private void runMultipleAsyncTask() // Run Multiple Async Task
    {
       LoadCountryJSON asyncTask = new LoadCountryJSON (); // First
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) // Above Api Level 13
        {
            asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
        else // Below Api Level 13
        {
            asyncTask.execute();
        }
        LoadStateJSON asyncTask2 = new LoadStateJSON (); // Second
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)// Above Api Level 13
        {
            asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
        else // Below Api Level 13
        {
            asyncTask2.execute();
        }
        LoadCityJSON asyncTask3 = new LoadCityJSON (); // Third
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)// Above Api Level 13
        {
            asyncTask3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
        else // Below Api Level 13
        {
            asyncTask3.execute();
        }
    }

I have gone through following links but it didn't help

http://blogs.innovationm.com/multiple-asynctask-in-android/

Two different AsyncTasks execute at the same time

Pradeep
  • 1,947
  • 3
  • 22
  • 45

3 Answers3

1

Try something like this:

// In AsyncTask1
protected void onPostExecute(Result someResult) {
    AsyncTask2 asyncTask2 = new AsyncTask2(someResult);
    // execute asyncTask2
}
algrid
  • 5,600
  • 3
  • 34
  • 37
1

You can look for a library like promise that will help you chain them if you would like or you can chain your own.

However, keep in mind that you are using ExecuteOnExecutor. By default AsyncTasks have only 1 thread and they wait on each other for the thread processing power to move. When you use the ExecuteOnExecutor and say threadPool you are saying new up new threads for each making them truly async.

However, even if you want your calls to be async, it seems like you want them to be syncronized so you have two options.

You can either simply do:

 new MyAsync1stTask() { 
    protected void onPostExecute(Boolean result) {
         new MyAsync2ndTask() { 
              protected void onPostExecute(Boolean result) {
                  //so on and so forth
              }
         }.execute(); // start the background processing
    }
}.execute(); // start the background processing

or

You can make a method and flags for completion like

private volatile boolean flag1;
private volatile boolean flag2;
private volatile boolean flag3;

private void loadSpinners(){
     new MyAsync1stTask() { 
        protected void onPostExecute(Boolean result) {
            flag1 = true;
            moveOnIfReady();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    new MyAsync2ndTask() { 
        protected void onPostExecute(Boolean result) {
            flag2 = true;
            moveOnIfReady();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    new MyAsync3rdTask() { 
        protected void onPostExecute(Boolean result) {
            flag3 = true;
            moveOnIfReady();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

private synchronized void moveOnIfReady(){
    if(flag1 && flag2 && flag3){
       releaseBusyIndicator()
       doStuff();
    }//else do nothing, they aren't all done yet
}
Sam
  • 5,342
  • 1
  • 23
  • 39
0

Chained calls. After receiving the response of one call, you call the next one and so on.

Pedro Varela
  • 2,296
  • 1
  • 26
  • 32
  • Can you please provide some snippet example or any references – Pradeep Sep 18 '17 at 20:07
  • Hey @Pradeep you are welcome buddy. If you want to become a great Android Developer, i recommend you to read as much as you can the Android documentation, there is a lot - a lot - to read but they explain details you sometimes don't find somewhere else. https://developer.android.com/guide/index.html. For instance, in the future if you need to rotate your screen, you will find out that AsyncTask won't suit you because they are cancelled on device rotation, read about Intent Services for instance. Best regards. Pedro – Pedro Varela Sep 21 '17 at 16:30
  • Thanks Pedro for your suggestion .Just jumped to ocean of android. Swimming is pending.Hope this link helps to swim with ease – Pradeep Sep 21 '17 at 16:34
  • @Pradeep Welcome then, don't worry, just keep swimming don't drown. Read, read, and share, that's how you master something. – Pedro Varela Sep 21 '17 at 16:36