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

前端 未结 17 2161
鱼传尺愫
鱼传尺愫 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:05

    This answer might be late but I would like to mention few things when your Activity dependent on AsyncTask. That would help you in prevent crashes and memory management. As already mentioned in above answers go with interface, we also say them callbacks. They will work as an informer, but never ever send strong reference of Activity or interface always use weak reference in those cases.

    Please refer to below screenshot to findout how that can cause issues.

    As you can see if we started AsyncTask with a strong reference then there is no guarantee that our Activity/Fragment will be alive till we get data, so it would be better to use WeakReference in those cases and that will also help in memory management as we will never hold the strong reference of our Activity then it will be eligible for garbage collection after its distortion.

    Check below code snippet to find out how to use awesome WeakReference -

    MyTaskInformer.java Interface which will work as an informer.

    public interface MyTaskInformer {
    
        void onTaskDone(String output);
    
    }
    

    MySmallAsyncTask.java AsyncTask to do long running task, which will use WeakReference.

    public class MySmallAsyncTask extends AsyncTask {
    
        // ***** Hold weak reference *****
        private WeakReference mCallBack;
    
        public MySmallAsyncTask(MyTaskInformer callback) {
            this.mCallBack = new WeakReference<>(callback);
        }
    
        @Override
        protected String doInBackground(String... params) {
    
            // Here do whatever your task is like reading/writing file
            // or read data from your server or any other heavy task
    
            // Let us suppose here you get response, just return it
            final String output = "Any out, mine is just demo output";
    
            // Return it from here to post execute
            return output;
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
    
            // Here you can't guarantee that Activity/Fragment is alive who started this AsyncTask
    
            // Make sure your caller is active
    
            final MyTaskInformer callBack = mCallBack.get();
    
            if(callBack != null) {
                callBack.onTaskDone(s);
            }
        }
    }
    

    MainActivity.java This class is used to start my AsyncTask implement interface on this class and override this mandatory method.

    public class MainActivity extends Activity implements MyTaskInformer {
    
        private TextView mMyTextView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mMyTextView = (TextView) findViewById(R.id.tv_text_view);
    
            // Start your AsyncTask and pass reference of MyTaskInformer in constructor
            new MySmallAsyncTask(this).execute();
        }
    
        @Override
        public void onTaskDone(String output) {
    
            // Here you will receive output only if your Activity is alive.
            // no need to add checks like if(!isFinishing())
    
            mMyTextView.setText(output);
        }
    }
    

提交回复
热议问题