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
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
{
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
}
}