Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AsyncTask - Order of execution

I am facing an issue regarding the order of exection of AsyncTasks.

My question is :

Assuming I have 2 implementations of AsyncTask : MyAsyncTask1 and MyAsyncTask2

That are called in the following manner :

new MyAsyncTask1 ().execute ();
new MyAsyncTask2 ().execute ();

Is it garanteed that MyAsyncTask1 is executed before MyAsyncTask2 ? One thing for sure, they are not executed in parallel since the default executor is used, which is SERIAL_EXECUTOR. The problem lies in the order of execution : which will be executed first ??

If the order of execution is not determined, how can I enforce an order of execution for the AsyncTasks ??

What I need is to have MyAsyncTask1 executed before MyAsyncTask2, which is not always the case, eventhough I am calling execute for MyAsyncTask1 before MyAsyncTask2.

What I actually have is a custom activity :

public abstract class CustomActivity extends Activity {

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        new MyAsyncTask2 ().execute ();
    }
}

And another activity that inherits from the custom activity :

public class MainActivity extends CustomActivity {

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        new MyAsyncTask1 ().execute ();
        super.onCreate ( savedInstanceState );
    }
}

So if I use the MainActivity, MyAsyncTask1 should be executed before MyAsyncTask2, at least that is the behavior I need.

like image 348
Leeeeeeelo Avatar asked Nov 05 '12 08:11

Leeeeeeelo


2 Answers

The only way to ensure that two threads (that's what AsyncTasks basically are) are executed in the order you want, is to start the second thread when the first one finishes.

In your case, to keep implementation abstracted and not have to actually call AsyncTask2 in the onPostExecute of AsyncTask1 (the way Anup and Sanket suggested, which is also fine if you want to mix them), make AsyncTask1 call super.executeAsyncTask2(), where executeAsyncTask2() is a method in your CustomActivity which starts the second AsyncTask

like image 84
zrgiu Avatar answered Oct 17 '22 15:10

zrgiu


In order to "chain" any number of AsyncTasks, what I do is have my AsyncTasks recieve a custom Callback as a parameter. You just have to define it like this:

public interface AsyncCallback(){
    public void onAsyncTaskFinished();
}

Your AsyncTask implementation constructor should have an AsyncCallback as a parameter, like this:

private AsyncCallback callback;

public AsyncTaskImplementation(AsyncCallback callback){
    //Do whatever you're doing here
    this.callback = callback;
}

Of course if they have more parameters, don't delete them.

Then, just before the end of onPostExecute, introduce this:

if (callback != null) callback.onAsyncTaskFinished();

So, if you pass a callback, the AsyncTask will execute your callback in the main thread. If you don't want to execute any callback, just pass null

So, to call your AsyncTask chain you just write this:

new AsyncTask1(new AsyncCallback(){

    public void onAsyncTaskFinished(){
         new AsyncTask2(null).execute();
    }

}).execute();

I hope this helps you ;-)

like image 44
Charlie-Blake Avatar answered Oct 17 '22 14:10

Charlie-Blake