1

I have an Activity A which has a button. Up on tapping on the button, it opens up another Activity B, which displays data fetched from server (http). The API version that I am using is 15.

As soon as the Activity B is opened, in onCreate() method, I am calling following lines of code.

        asyncFetch = new AsyncFetch();
        asyncFetch.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

This Async task performs http communication to fetch data from server.

    @Override
    protected Object doInBackground(Object... params) {
        ...
        defaultHttpClient = new DefaultHttpClient();
        httpPost = new HttpPost(theURL);
        ...
        HttpResponse response = defaultHttpClient.execute(httpPost);
        ...
        return response...;
    }

Once data is fetched, the UI is populated.

onBackPressed() and home button, I am calling the following code to cancel the Async task.

            asyncFetch.cancel(true);

When a back button or home button is tapped, the Async task is not getting cancelled and UI waits for the Async task to complete and then it goes back to parent Activity.

The reason of not using the execute() is because I have couple of Async tasks that are running which I don't want this task to wait for other Async task to complete.

Can someone tell me how to cancel this Async task? Any help is appreciated.

User12111111
  • 1,179
  • 1
  • 19
  • 41
  • AsyncTasks, are something I am seeing being used less and less. They have problems staying with the Activity life cycle. I would suggest Volley (which is very good) or using an IntentService as these do not get so involved with the life cycle. – Graham Smith Jul 03 '14 at 10:40
  • 1
    http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing-asynctask – Sergey Benner Jul 03 '14 at 10:41
  • To add to @SergeyBenner s comment, look at the second answer specifically – Mario Stoilov Jul 03 '14 at 10:44

2 Answers2

1

if you are using this

asyncFetch.cancel(true);

Then you need to check in doInBackGround() method that does user set cancel to true . There is a method define in asyntask , isCancelled().you need to check it like.

if(isCancelled())
//break out of doInBackground and handle this case

Android documentation says about isCancelled().

Returns true if this task was cancelled before it completed normally. If you are calling cancel(boolean) on the task, the value returned by this method should be checked periodically from doInBackground(Object[]) to end the task as soon as possible.

you can check documentation here too.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

You need to update your doInBackgraopund() method like this:

 @Override
    protected Object doInBackground(Object... params) {

       if (isCancelled()) {   // Always check for satus of your AsnycTask sataus. if it cancel break from loop.

          break;  // As long as your AsyncTask has cancel it will stop processing

       }
       else {
         // do your work here
      }

}
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85