Advertisement
kamoka

How to get the result on OnPostExecute() to main activity

Dec 15th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. Easy:
  2.  
  3. Create interface class, where String output is optional, or can be whatever variables you want to return.
  4.  
  5. public interface AsyncResponse {
  6.     void processFinish(String output);
  7. }
  8. Go to your AsyncTask class, and declare interface AsyncResponse as a field :
  9.  
  10. public class MyAsyncTask extends AsyncTask{
  11.   public AsyncResponse delegate = null;
  12.  
  13.     @Override
  14.     protected void onPostExecute(String result) {
  15.       delegate.processFinish(result);
  16.     }
  17.  }
  18. In your main Activity you need to implements interface AsyncResponse.
  19.  
  20. public class MainActivity implements AsyncResponse{
  21.   MyAsyncTask asyncTask =new MyAsyncTask();
  22.  
  23.   @Override
  24.   public void onCreate(Bundle savedInstanceState) {
  25.  
  26.      //this to set delegate/listener back to this class
  27.      asyncTask.delegate = this;
  28.  
  29.      //execute the async task
  30.      asyncTask.execute();
  31.   }
  32.  
  33.   //this override the implemented method from asyncTask
  34.   @Override
  35.   void processFinish(String output){
  36.      //Here you will receive the result fired from async class
  37.      //of onPostExecute(result) method.
  38.    }
  39.  }
  40. UPDATE
  41.  
  42. I didn't know this is such a favourite to many of you. So here's the simple and convenience way to use interface.
  43.  
  44. still using same interface. FYI, you may combine this into AsyncTask class.
  45.  
  46. in AsyncTask class :
  47.  
  48. public class MyAsyncTask extends AsyncTask{
  49.  
  50.   // you may separate this or combined to caller class.
  51.   public interface AsyncResponse {
  52.         void processFinish(String output);
  53.   }
  54.  
  55.   public AsyncResponse delegate = null;
  56.  
  57.     public MyAsyncTask(AsyncResponse delegate){
  58.         this.delegate = delegate;
  59.     }
  60.  
  61.     @Override
  62.     protected void onPostExecute(String result) {
  63.       delegate.processFinish(result);
  64.     }
  65. }
  66. do this in your Activity class
  67.  
  68. public class MainActivity extends Activity {
  69.  
  70.    MyAsyncTask asyncTask =new MyAsyncTask(new AsyncResponse(){
  71.  
  72.      @Override
  73.      void processFinish(String output){
  74.      //Here you will receive the result fired from async class
  75.      //of onPostExecute(result) method.
  76.      }
  77.   }).execute();
  78.  
  79.  }
  80. Or, implementing the interface on the Activity again
  81.  
  82. public class MainActivity extends Activity
  83.     implements AsyncResponse{
  84.  
  85.     @Override
  86.     public void onCreate(Bundle savedInstanceState) {
  87.  
  88.         //execute the async task
  89.         new MyAsyncTask(this).execute();
  90.     }
  91.  
  92.     //this override the implemented method from AsyncResponse
  93.     @Override
  94.     void processFinish(String output){
  95.         //Here you will receive the result fired from async class
  96.         //of onPostExecute(result) method.
  97.     }
  98. }
  99. 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement