You can create a separate private private package of abstract packages by extending AsyncTask and implementing the doInBackground() method:
abstract class MyAsyncTask extends AsyncTask<Void, Void, String> { @Override final protected String doInBackground(Void... progress) {
And in your actions, just inherit from MyAsyncTask (by the way, the new class should probably be private), implementing the onPostExecute() and onPreExecute() methods:
public class Activity_1 extends BaseActivity { ... new Async1().execute(); ... private class Async1 extends MyAsyncTask { @Override protected void onPreExecute(){
If onPreExecute and onPostExecute contain some common actions, you can apply the following pattern:
abstract class MyAsyncTask extends AsyncTask<Void, Void, String> { 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) {
and use it in their activities as follows:
public class Activity_1 extends BaseActivity { ... MyAsyncTask aTask = new MyAsyncTask(); aTask.setListener(new MyAsyncTask.MyAsyncTaskListener() { @Override void onPreExecuteConcluded() {
You can also use Activity MyAsyncTaskListener MyAsyncTaskListener :
public class Activity_1 extends BaseActivity implements MyAsyncTask.MyAsyncTaskListener { @Override void onPreExecuteConcluded() {
I wrote the code from my head, so it may contain errors, but it should illustrate the idea.
source share