Question: 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 :

```java
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.

Answer: 
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 ;-)
Don't make things complicated and try to keep your design simple and clean. The race between your both AsyncTasks depends on the amount of processing they carry inside their overrided methods. Logically, if you want your AsyncTask1 to complete before AsyncTask2, then simply add the code-blocks of your second Async inside the first one.

Or launch your second Async from the **onPostExecute** of your first Async - but personally, that seems unnecessary.