Call AsyncTask from another class

后端 未结 4 1347
野性不改
野性不改 2020-12-24 04:17

In an existing app I have an activity with an inner class which extends AsyncTask, this looks like the following:

public class Activity_1 extends BaseActivit         


        
4条回答
  •  独厮守ぢ
    2020-12-24 04:43

    You can make a separate abstract package private class, extending AsyncTask and implementing doInBackground() method:

    abstract class MyAsyncTask extends AsyncTask {
        @Override
        final protected String doInBackground(Void... progress) { 
            // do stuff, common to both activities in here
        }
    }
    

    And in your activities just inherit from MyAsyncTask (new class probably should be private, by the way), implementing onPostExecute() and onPreExecute() methods:

    public class Activity_1 extends BaseActivity {
    
        ...
        new Async1().execute();
        ...
    
        private class Async1 extends MyAsyncTask {
            @Override
            protected void onPreExecute(){ 
                // Activity 1 GUI stuff
            }
    
            @Override
            protected void onPostExecute(String result) {
                // Activity 1 GUI stuff
            }
        }
    }
    

    If onPreExecute and onPostExecute contain some common actions as well, you can apply the following pattern:

    abstract class MyAsyncTask extends AsyncTask {
        public interface MyAsyncTaskListener {
           void onPreExecuteConcluded();
           void onPostExecuteConcluded(String result);  
        }
    
        private MyAsyncTaskListener mListener;
    
        final public void setListener(MyAsyncTaskListener listener) {
           mListener = listener;
        }
    
        @Override
        final protected String doInBackground(Void... progress) { 
            // do stuff, common to both activities in here
        }
    
        @Override
        final protected void onPreExecute() {
            // common stuff
            ...
            if (mListener != null) 
                mListener.onPreExecuteConcluded();
        }
    
        @Override
        final protected void onPostExecute(String result) {
            // common stuff
            ...
            if (mListener != null) 
                mListener.onPostExecuteConcluded(result);
        }
    }
    

    and use it in your activity as following:

    public class Activity_1 extends BaseActivity {
    
        ...
        MyAsyncTask aTask = new MyAsyncTask();
        aTask.setListener(new MyAsyncTask.MyAsyncTaskListener() {
           @Override
           void onPreExecuteConcluded() {
               // gui stuff
           }
    
           @Override
           void onPostExecuteConcluded(String result) {
               // gui stuff
           }
        });
        aTask.execute();
        ...
    }    
    

    You can also have your Activity implement MyAsyncTaskListener as well:

    public class Activity_1 extends BaseActivity implements MyAsyncTask.MyAsyncTaskListener {
        @Override
        void onPreExecuteConcluded() {
            // gui stuff
        }
    
        @Override
        void onPostExecuteConcluded(String result) {
           // gui stuff
        }
    
        ...
        MyAsyncTask aTask = new MyAsyncTask();
        aTask.setListener(this);
        aTask.execute();
        ...
    
    }
    

    I wrote the code from the head, so it might contain errors, but it should illustrate the idea.

提交回复
热议问题