[Solved] Android AsyncTask PostExecute [closed]


If you want to perform some function once the Asynctask is complete, you can setup a callback function in the calling activity. Create a constructor in the AsyncTask class and pass it the calling activity. Then use this reference to call the callback function when your task is complete.

You can also update the UI from within onPostExecute, which is simpler if it works for you.

public class MyTask extends AsyncTask<String, Void, String>
{
    private MainActivity activity;

    public MyTask(MainActivity activity) {
        this.activity = activity;
    }
    @Override
    protected Void doInBackground(String... params) {
        //Do background stuff
    }

    protected void onPostExecute() {
        //dismiss progress dialog if needed
        //Callback function in MainActivity to indicate task is done
        activity.taskDone("some string");
    }
}

MainActivity.java

//Pass your params array and the current activity to the AsyncTask
new MyTask(MainActivity.this).execute(params);

//Callback for AsyncTask to call when its completed
public void taskDone(String returnVal) {
    //Do stuff once data has been loaded
    returnText = returnVal;
}

1

solved Android AsyncTask PostExecute [closed]