使用嵌套 AsyncTasks 的最佳实践是什么?

What is the best practice to use nested AsyncTasks?

在我的 Android 应用程序中,我有多达 4 个相互依赖的异步任务,这意味着一个任务必须先完成,然后下一个任务才能使用检索到的数据。现在,当代码看起来像这样时,这在某些时候可能会很不清楚:

final AsyncTask<Void, Void, Boolean> taskOne = new AsyncTask<Void, Void, Boolean>() {   
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // retrieve required data   
        return true;
    }

    @Override
    protected void onPostExecute(Boolean success) {
        if (success) {
            // start second task here
            final AsyncTask<Void, Void, Boolean> taskTwo = new AsyncTask<Void, Void, Boolean>() {
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                }

                @Override
                protected Boolean doInBackground(Void... params) {
                    // retrieve required data   
                    return true;
                }

                protected void onPostExecute(Boolean success) {
                    if (success) {
                        // start third task here
                        final AsyncTask<Void, Void, Boolean> taskThree = new AsyncTask<Void, Void, Boolean>() {
                            @Override
                            protected void onPreExecute() {
                                super.onPreExecute();
                            }

                            @Override
                            protected Boolean doInBackground(Void... params) {
                                // retrieve required data   
                                return true;
                            }

                            protected void onPostExecute(Boolean success) {
                                if (success) {
                                    // and so on ...
                                }
                            }
                        }

                        taskThree.execute();
                    }
                }
            }

            taskTwo.execute();
        }
    }
}

taskOne.execute();

使用更具可读性的代码实现此行为的最佳做法是什么?

提前致谢

我认为你应该崩溃界面。在您的情况下,它称为侦听器或回调。我没有测试代码。但是看起来像那样

    Class A extend Activity implement ServerRequestListener{


          public ServerRequestListener listener;

          @Override 
          onCreate (Bundle bundle){
               listener = this;
               doTaskOne();
          }

          public void doTaskOne(){
               final AsyncTask<Void, Void, Boolean> taskOne = new AsyncTask<Void, Void, Boolean>() {   
                     @Override
                     protected void onPreExecute() {
                         super.onPreExecute();
                     }

                     @Override
                     protected Boolean doInBackground(Void... params) {
                         // retrieve required data   
                         return true;
                     }

                     @Override
                     protected void onPostExecute(Boolean success) {
                         if (success) {
                             listener.onSuccess(new JSON());
                         }
                     }
            }

            taskOne.execute();

        }



         public interface ServerRequestListener {
               void onSuccess(Json);
               void onFailure(Error);
         }


         @Override
         onSuccess(Json json){
             //call second task
         }

         @Override
         onFailure(Error error){

         }

    }

任务一

Class TaskOne extends AsyncTask{

  onPostExecute(boolean success){
     if(success){
        new TaskTwo().execute();
     }
  }
}

任务二

Class TaskTwo extends AsyncTask{

  onPostExecute(boolean success){
     if(success){
        new TaskThree().execute();
     }
  }
}

任务三

Class TaskThree extends AsyncTask{

      onPostExecute(boolean success){
         if(success){
            //do something
         }
      }
}