How to cancel Asysnctask in android
<p>I am running some tasks in UI part of my application.</p>

<p>While doing this lengthy task I need to show progress dialog so I am using AsysnTask to perform this.</p>

<p>When user pressed back button while progress bar going on , I  need to cancel progress bar and I need to suspend that background work going on.</p>

<p>I am calling "dismiss()" on Progress Dialog so it's dismissed.</p>

<p>I am calling "cancel(true)" on my AsysnTask object, I thought my backgroung operations got stopped , but still it is executing though I have called "cancel(true)".</p>

<p>Then I have done some search on this issue and came to know that "cancel(true)"  only called "onCancelled()" method of the AsyncTask.</p>

<p>I don't know what I have to write in my onCancelled() method so that my background operations got stopped immediately.</p>

<p>My doInBackground() method something look like this:</p>

<pre><code>      protected void doInBackground()
         {
                  Statement  1;
                  Statement  2;
                 Statement  3;
                 Statement  4;
                 Statement  5;
                 Statement  6;
                 Statement  6;
                 Statement  7;
                 Statement  8;
                 Statement  9;

          }
</code></pre>

<p>let say in above code If got request to cancel after "Statement  4" then I don't want to execute reaming  statements.</p>

<p>The above "doInBackground()" is just a example to make it understandable , In actual my "doInbackground()" method will have 100's of statements and I cant predict when I can get the request to cancel the operation.</p>

<p>Please help me how to do that.</p>
