title: Android: Can not cancel an Async Task
body: <p>I have an <code>Activity</code> A which has a button. Up on tapping on the button, it opens up another <code>Activity</code> B, which displays data fetched from server (http). The <code>API</code> version that I am using is 15.</p>
  
  <p>As soon as the <code>Activity</code> B is opened, in <code>onCreate()</code> method, I am calling following lines of code. </p>
  
  <pre><code>        asyncFetch = new AsyncFetch();
          asyncFetch.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  </code></pre>
  
  <p>This <code>Async</code> task performs <code>http</code> communication to fetch data from server. </p>
  
  <pre><code>    @Override
      protected Object doInBackground(Object... params) {
          ...
          defaultHttpClient = new DefaultHttpClient();
          httpPost = new HttpPost(theURL);
          ...
          HttpResponse response = defaultHttpClient.execute(httpPost);
          ...
          return response...;
      }
  </code></pre>
  
  <p>Once data is fetched, the UI is populated.</p>
  
  <p><code>onBackPressed()</code> and <code>home</code> button, I am calling the following code to cancel the <code>Async</code> task. </p>
  
  <pre><code>            asyncFetch.cancel(true);
  </code></pre>
  
  <p>When a back button or home button is tapped, the <code>Async</code> task is not getting cancelled and UI waits for the Async task to complete and then it goes back to parent Activity. </p>
  
  <p>The reason of not using the <code>execute()</code> is because I have couple of <code>Async</code> tasks that are running which I don't want this task to wait for other <code>Async</code> task to complete.</p>
  
  <p>Can someone tell me how to cancel this Async task? Any help is appreciated.</p>