title: Async task Android execute
body: <p>This was asked in one of the Android interviews. I was asked whether it's possible to start another async task (let it be Task2) from doInBackground() method of async task 1(let it be Task1). I had gone through the docs which say the following:</p>
  
  <blockquote>
    <p>The task instance must be created on the UI thread. </p>
    
    <p>execute(Params...) must be invoked on the UI thread.</p>
  </blockquote>
  
  <p>As per these statements, I think that it shouldn't be possible to start a task from background method of another task. Also, async task has UI methods (which cannot be used on a background thread), so that strengthened my argument and I answered it as not possible. </p>
  
  <p>On checking on a simple demo app, I saw that it's indeed possible to do so.
  Some demo code: </p>
  
  <pre><code> @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mContext = this;
          init();
          Log.v ("gaurav", "Thread is : " + Thread.currentThread().getName());
          Task1 task = new Task1();
          task.execute();
      }
  
  class Task1 extends AsyncTask {
      @Override
      protected Object doInBackground(Object... params) {
          // TODO Auto-generated method stub
          Log.v ("gaurav", "Thread task 1 is : " + Thread.currentThread().getName());
  
          Task2 task = new Task2();
  
          task.execute();
          return null;
      }
  }
  
  class Task2 extends AsyncTask {
      @Override
      protected Object doInBackground(Object... params) {
          // TODO Auto-generated method stub
          Log.v ("gaurav", "Thread task 2 is : " + Thread.currentThread().getName());
  
          Log.v ("gaurav", "Task 2 started");
          return null;
      }
  }
  </code></pre>
  
  <p>I get following logs indicating successful execution : </p>
  
  <pre><code>&gt; 08-07 09:46:25.564: V/gaurav(2100): Thread is : main 08-07
  &gt; 09:46:25.564: V/gaurav(2100): Thread task 1 is : AsyncTask #3 08-07
  &gt; 09:46:25.564: V/gaurav(2100): Thread task 2 is : AsyncTask #4 08-07
  &gt; 09:46:25.564: V/gaurav(2100): Task 2 started
  </code></pre>
  
  <p>I have checked this on ICS, KK and L device and it works fine for all.</p>
  
  <p>One reason I could think of is that I'm not overriding any UI methods and doing any UI updation in my second task, hence it doesn't cause any problems, but I'm not sure. Even if that's the case, it violates the threading rules mentioned in the developer guide.</p>
  
  <p>As a reference, I checked out this link too : <a href="https://stackoverflow.com/questions/19520668/start-asynctask-from-another-asynctask-doinbackground">Start AsyncTask from another AsyncTask doInBackground()</a> but the answer states to start the second task using runOnUiThread() method inside doInBackground().
   I'd like some help on what's going on here. Thanks.</p>