1

I would like to call an Activity method after the onPostExecute of my AsyncTask. Do you know how I can do that?

I want to call in the sendSMS(String phoneNumber, String message) method in the onPostExecute.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Shuty
  • 87
  • 1
  • 4
  • 9

4 Answers4

4

One way is to pass an instance of the Activity through PostTask constructor, something like:

private class PostTask extends AsyncTask<String, Integer, String>
{
    private AsyncBigCalculActivity activity;

    public PostTask(AsyncBigCalculActivity activity)
    {
        this.activity = activity;
    }

    // ...
}

and on creating the PostTask instance, pass the activity instance:

new PostTask(this).execute();

Now you can invoke sendSMS() from within PostTask, like:

activty.sendSMS(...);

Also note that if you are defining the PostTask as a private class inside the activty, then you can invoke sendSMS() like:

AsyncBigCalculActivity.this.sendSMS(...);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

Add a constructor and a global variable to your AsyncTask like this:

AsyncBigCalculActivity mActivity;

public PostTask(AsyncBigCalculActivity a) {
    mActivity = a;
}

Then simply use mActivity.sendSMS("test", "test") when you need it.

However, you should really have methods like sendSMS() in a utility class.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
1

If your AsyncTask is an inner class of your Activity then you should be able to call the Activity method from your onPostExecute(). Otherwise, you can send the Context to a constructor of your AsyncTask and uses that to call the method

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

Write a Callback

You can create a CallBack using an interface. This way you can use your AsyncTask with any activity. (Loosely coupled code)

1) Create a Callback

interface MyAsyncTaskCallBack{
 
   public void doStuff(String arg1,String arg2);

}  

2) Initialize the callback in your AsyncTask

private class MyTask extends AsyncTask<String, Void, Void>
{
     private MyAsyncTaskCallBackactivity callback;

     public MyTask(MyAsyncTaskCallBackactivity callback)
     {
          this.callback = callback;
     }

     //Call callback.doStuff(....phonenum, ....message); in your postExecute

}

3) Implement the Callback in your Activity and override doStuff() method

public YourActivity extends AppCompatActivity implements MyAsyncTaskCallBack{

      // Your Activity code 
      // new MyTask(this).execute("phonenum","msg");    //<--- This is how you run AsyncTask

      private void sendMessage(String num, String msg){

          // send msg logic
      }

     @Override
     public void doStuff(String arg1,String arg2){
         sendMessage(arg1,arg2);  // invoke activity method
     }

} 
Community
  • 1
  • 1
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
  • can you help with how to call Activity methods in the below call. As when i tried to call method it doesn't recognize it. I have callback in Activity new MyTask(new MyAsyncTaskCallBack(){ @Override public void doStuff(String result){ //Do something this.activitymethod() // it doesn't recognize this method of activity though it is calling inside the activity. } }, this.getApplication().getBaseContext()).execute(params); – Mr. Jay Nov 05 '20 at 16:09
  • In your activity create MyTask like this. --> new MyTask(this).execute(params) @Mr.Jay – Rohit Singh Nov 05 '20 at 20:15