Android calling AsyncTask right after an another finished

后端 未结 7 2234
萌比男神i
萌比男神i 2020-11-28 06:15

I have some problem with Android AsyncTask. There is an Activity which contains some TextView a button and a picture. When an user entered this activity I start an asynctask

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 06:29

    I have an idea to make async series in just one async task:

    protected Boolean doInBackground(String... params) {
                if(params[0] == "taskA") {
                  //do somthing
                  params[0] = "taskB";
                }
                if(params[0] == "taskB") {
                  //do somthing
                  params[0] = "taskC";
                }
                if(params[0] == "taskC") {
                  //do somthing
                  params[0] = "taskD";
                }
                if(params[0] == "taskD") {
                  //do somthing
                  return true;
                }
    

    And in your main thread just call async task like this:

    ShowMyProgress(); //if you like
    new MyAsyncTask().execute("taskA");
    

    And finally you can hide your progress on onPostExecute like:

    protected void onPostExecute(final Boolean success) {
            if (success) {
                ....
    
                HideMyProgress();
            }
    }
    

提交回复
热议问题