Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run another AsyncTask in onPostExecute()

What if I need to run another async task being in another async task, meaning in onPostExecute() method, how to do so?

like image 333
Eugene Avatar asked Apr 22 '13 12:04

Eugene


People also ask

Can we execute another AsyncTask from another AsyncTask's doInBackground () method?

It is possible but it will crash if you try to modify the UI from the onPostExecute on the second task as it is not executing in the main thread. IF your second task not contains UI operation, you can start. Its advisable to start async in post execute of first async.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

How do I run AsyncTask?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.


1 Answers

As a recommendation, try to off-load as many time's taking calls in your first AsyncTask as you can. However, if your application design is in such a way that you only need to execute another task once the first task is completed then simply execute the second AsyncTask exactly in the same way as you are doing for the first one.

@Override
protected void onPostExecute(String result) {
     new MySecondAsyncTask().execute(params);   //params if any
}
like image 142
waqaslam Avatar answered Oct 25 '22 00:10

waqaslam