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: 
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
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 ;-)