How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

前端 未结 17 2208
鱼传尺愫
鱼传尺愫 2020-11-21 04:50

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute(

17条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 05:16

    Easy:

    1. Create interface class, where String output is optional, or can be whatever variables you want to return.

      public interface AsyncResponse {
          void processFinish(String output);
      }
      
    2. Go to your AsyncTask class, and declare interface AsyncResponse as a field :

      public class MyAsyncTask extends AsyncTask {
        public AsyncResponse delegate = null;
      
          @Override
          protected void onPostExecute(String result) {
            delegate.processFinish(result);
          }
       }
      
    3. In your main Activity you need to implements interface AsyncResponse.

      public class MainActivity implements AsyncResponse{
        MyAsyncTask asyncTask =new MyAsyncTask();
      
        @Override
        public void onCreate(Bundle savedInstanceState) {
      
           //this to set delegate/listener back to this class
           asyncTask.delegate = this;
      
           //execute the async task 
           asyncTask.execute();
        }
      
        //this override the implemented method from asyncTask
        @Override
        void processFinish(String output){
           //Here you will receive the result fired from async class 
           //of onPostExecute(result) method.
         }
       }
      

    UPDATE

    I didn't know this is such a favourite to many of you. So here's the simple and convenience way to use interface.

    still using same interface. FYI, you may combine this into AsyncTask class.

    in AsyncTask class :

    public class MyAsyncTask extends AsyncTask {
    
      // you may separate this or combined to caller class.
      public interface AsyncResponse {
            void processFinish(String output);
      }
    
      public AsyncResponse delegate = null;
    
        public MyAsyncTask(AsyncResponse delegate){
            this.delegate = delegate;
        }
    
        @Override
        protected void onPostExecute(String result) {
          delegate.processFinish(result);
        }
    }
    

    do this in your Activity class

    public class MainActivity extends Activity {
    
       MyAsyncTask asyncTask = new MyAsyncTask(new AsyncResponse(){
    
         @Override
         void processFinish(String output){
         //Here you will receive the result fired from async class 
         //of onPostExecute(result) method.
         }
      }).execute();
    
     }
    

    Or, implementing the interface on the Activity again

    public class MainActivity extends Activity 
        implements AsyncResponse{
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            //execute the async task 
            new MyAsyncTask(this).execute();
        }
    
        //this override the implemented method from AsyncResponse
        @Override
        void processFinish(String output){
            //Here you will receive the result fired from async class 
            //of onPostExecute(result) method.
        }
    }
    

    As you can see 2 solutions above, the first and third one, it needs to create method processFinish, the other one, the method is inside the caller parameter. The third is more neat because there is no nested anonymous class. Hope this helps

    Tip: Change String output, String response, and String result to different matching types in order to get different objects.

提交回复
热议问题