-2

I am making a http request using AsyncTask and what I want is that when I logout, the AsyncTask should stop. so I am calling cancel(true) in onStop(). When I call cancel(true), the requests that are not started yet, are cancelled, but the problem is that isCancelled() is never called which checks if the executing task is cancelled or not. I am checking isCancelled() in doInBackgroud() method of AsyncTask Is there a way to stop the executing AsyncTask. Following is the scenario.

class AsyncClass extends AsyncTask<>{

    @Override
     protected String doInBackground(Void... params)
      {
             if(isCancelled())
               {
                Log.d("isCancelled", iscancelled());
               }
          //call the webservice

      }
    }

Now there is some other class from where I'm calling

if(asyncTaskObject!=null){
    asyncTaskObject.cancel(true);
        asyncTaskObject=null;
}

But Log statement inside iscancelled() is never called.

Rookie
  • 8,660
  • 17
  • 58
  • 91
  • 1
    After your last edit it is totally unclear what you are asking for. – Robert Jun 29 '12 at 09:33
  • Robert is correct. I can only assume you mean onCancelled not isCancelled. Look at this: http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing-asynctask – Kuffs Jun 29 '12 at 09:40
  • http://stackoverflow.com/questions/2531336/asynctask-wont-stop-even-when-the-activity-has-destroyed. Can u check this. – Rookie Jun 29 '12 at 09:42
  • For your latest edit, you are checking isCancelled after that part of the code has already executed. By the time isCancelled would return True, you are already calling your web service. – Kuffs Jun 29 '12 at 09:50
  • What could be the way around. ? – Rookie Jun 29 '12 at 09:51
  • Rest of the code is in a different class. So i dont think i can check isCancelled() there. Can I do something else? – Rookie Jun 29 '12 at 09:53
  • If you are executing one web request, forget about cancelling it. If there are multiple requests, check isCancelled before each one and exit if it is true. – Kuffs Jun 29 '12 at 09:54
  • I want to cancel the task that is currently executing. – Rookie Jun 29 '12 at 09:58
  • Because what happens is when I logout, that task is still running and returns me the data and when I login using different user, data for previous user is shown. – Rookie Jun 29 '12 at 10:05

1 Answers1

1

Why do you expect that isCancelled() should be executed by Android itself. This is a getter method that allows your code to query the status of the task.

Robert
  • 39,162
  • 17
  • 99
  • 152
  • That is not correct, you can stop an AsyncTask. If you call cancel(true), an interrupt will be sent to the background thread, which may help interruptible tasks. Otherwise, you should simply make sure to check isCancelled() regularly in your doInBackground() method. ...This the comment by romain guy – Rookie Jun 29 '12 at 09:10
  • 1
    Your comment is contradictory. First you say that I am wrong - later you say that isCanceled() have to be called from your own code which is exactly what I wrote. – Robert Jun 29 '12 at 09:25
  • You have changed the question after I answered it. – Robert Jun 29 '12 at 09:34