Call AsyncTask from another class

In an existing application, I have activity with an inner class that extends AsyncTask, it looks like this:

public class Activity_1 extends BaseActivity { .... new async().execute(); ... public class asyncextends AsyncTask<Void, Void, String> { protected String doInBackground(Void... progress) { ... } protected void onPreExecute() { ... } protected void onPostExecute(String result) { ... } } } 

Now I need to call the same doInBackground method from another action, but the onPostExecute() this inner class works with some local variables of the user interface and, therefore, it is impossible to use it from outside clas. Is there a way I can call this AsyncTask and just override the onPostExecute and onPreExecute , or should I create another inner class in another action, do the same (for example, move it to a generic useful class or something else) etc.??

+6
source share
4 answers

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) { // do stuff, common to both activities in here } } 

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(){ // Activity 1 GUI stuff } @Override protected void onPostExecute(String result) { // Activity 1 GUI stuff } } } 

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) { // 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 their activities as follows:

 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 use Activity MyAsyncTaskListener MyAsyncTaskListener :

 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 my head, so it may contain errors, but it should illustrate the idea.

+19
source

It's that simple. Just just create an object of the main class and call the inner class as follows

  OuterMainClass outer = new OuterMainClass(); outer.new InnerAsyncClass(param) .execute(); 

this answer is too late to help you, but hope it helps others.

thanks

+11
source

1. Create an AsynckTask constructor in ClassOne.

2.Crate or ClassOne object for a new keyword.

3. Define the Async task by object

 ClassOne{ class AsyncParmas extends AsyncTask { public ADDloadGeofenceDetails() { } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Object doInBackground(Object... params) { } } Class ClassTwo{ ClassOne obj= new ClassOne (); obj.new AsyncParmas ().execute(); } } 

GoodLuck Who ran into a problem.

0
source

If we create one static method that resides in one class and will be executed in any class in doInBackground AsyncTask, we can easily update the interface through the same class and even in another class.

-1
source

Source: https://habr.com/ru/post/944980/


All Articles