YVS1102
YVS1102

Reputation: 2748

Running asynctasks one by one

I have 3 asynctask to run.

new getNewArrivalProducts().execute();
new getPromo().execute();
new getUserinfo().execute();

It works well, But now i want to run it one by one. So i try this

final AsyncTask First  = new getNewArrivalProducts().execute();
final AsyncTask Second = new getPromo();
final AsyncTask Third = new getUserinfo();

     if(First.getStatus() == AsyncTask.Status.RUNNING){
                            inAnimation = new AlphaAnimation(0f, 1f);
                            inAnimation.setDuration(200);
                            progressBarHolder.setAnimation(inAnimation);
                            progressBarHolder.setVisibility(View.VISIBLE);
                        }

                        if(First.getStatus() == AsyncTask.Status.FINISHED && Second.getStatus() != AsyncTask.Status.RUNNING){
                            Second.execute();
                            if(Second.getStatus() == AsyncTask.Status.FINISHED  && Third.getStatus() != AsyncTask.Status.RUNNING){
                                Third.execute();
                                if(Third.getStatus() == AsyncTask.Status.FINISHED ){
                                    outAnimation = new AlphaAnimation(1f, 0f);
                                    progressBarHolder.setAnimation(outAnimation);
                                    progressBarHolder.setVisibility(View.INVISIBLE);
                                }
                            }
                        }

But, with my script above. Only first is running and the others are not running. How can i make my asynctasks run by one ? thanks in advance

Upvotes: 1

Views: 30

Answers (1)

Quick learner
Quick learner

Reputation: 11457

Try this

Call the asyntask one by one in post execute methods accordingly

First call this

new getNewArrivalProducts().execute();

then in post execute call new getPromo().execute();

public class getNewArrivalProducts extends AsyncTask<Void, Void, String> {

  // rest of the code here

    @Override
    protected void onPostExecute(String result) {
      new getPromo().execute();
    }
 }

public class getPromo extends AsyncTask<Void, Void, String> {

   // rest of the code here

    @Override
    protected void onPostExecute(String result) {
      new getUserinfo().execute();
    }
 }

public class getUserinfo extends AsyncTask<Void, Void, String> {

 // rest of the code here

    @Override
    protected void onPostExecute(String result) {

    }
 }

Upvotes: 2

Related Questions