1

I had an issue recently where I called an asynctask from an activity and returned some data. My UI would freeze up and I was getting an ANR. I seemed to have resolved it by using a combination of solutions for both things I thought it could be, both the asynctask and the arraylist I was implementing.

I had a callback on the asynctask to load the data asynchronously. I guessed, though I had a hard time figuring out, that the asynctask was being retained after onpostexecute? In any case, I called this.cancel() in onpostexecute.

After reading up a bit on it, it seems that the cancel() function on schedules the task to be cancelled and that it can possibly persist after the request is made. So I must ask is there a better way to cancel the asynctask after you are finished with it? Will cancel() be sufficient in most cases? Are there ways to avoid having to call cancel() at all while implementing a call back.

user2097211
  • 333
  • 2
  • 5
  • 16
  • What you are doing(`calling this.cancel()` in `onPostExecute()`) is redundant. Once execution has reached `onPostExecute()`, the thread waits for the garbage collector. If another `AsyncTask` needs a thread, this thread may be recycled. But you should not be worrying about it. The question here is: Why is your `AsyncTask` typing up the UI? – Vikram Jul 19 '13 at 17:33
  • @vikram there is a decent chance I was wrong about it being an asynctask problem and it could have been my ArrayList. It seemed like scheduling this.cancel was redundant but I wasn't sure. I wasn't really sure of how it could be retained and tying up the UI after onpostexecute is called. My UI ANR doesn't happen until after onpostexecute is called. – user2097211 Jul 19 '13 at 18:06
  • you can post another question to get help on the ANR issue. – Vikram Jul 19 '13 at 18:24
  • @vikram http://stackoverflow.com/questions/17735305/listliststring-return-from-asynctask-with-callback-implementation-ui-freezi I solved the problem by changing the asynctaks to return and arraylist> and called this.cancel() in asynctask – user2097211 Jul 19 '13 at 18:28

4 Answers4

0

What I have done in the past is set a synchronized boolean that the async task and its caller can see. When I want to cancel the async task I set the boolean to false (normally named active). Inside the async task, I check the flag at various points and if it is ever false (meaning it has been cancelled) then I just return without completing the rest of the task.

Without seeing your specific situation it is hard to be more specific.

Hope it helps, Mike

Mike Baglio Jr.
  • 1,990
  • 3
  • 18
  • 19
  • do you implement that synchronized boolean in a callback you create for the asynctasks? That sounds like a good idea, especially the return provision without necessarily calling cancel. – user2097211 Jul 19 '13 at 17:43
  • See [this post](http://stackoverflow.com/questions/17724048/asynctask-get-no-progress-bar/17725922?noredirect=1#comment25840553_17725922) I just gave an example of this yesterday to someone trying to cancle a task. The basic code sample is there. – Rarw Jul 19 '13 at 18:08
0

If you place some line of code in doInBackground that only executed once and not in loop,there is no way to cancel it after executing your task.But if you have loop in it or other commands that run periodically,you can use setCancel(true) for your task and check the value of isCanceled() of task before doing your statement in loop.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
0

I had a similar problem like you

check this , about the canceling of a asynctask

Also read this about the callback. Maybe if you overwrite the onPostExecute() you can do things easier in your life.

Community
  • 1
  • 1
malcolm the4
  • 347
  • 2
  • 5
  • 15
0

Using a boolean variable is what you need to do as already suggested. But don't implement that by yourself since the Android framework already does this for you. Use AsyncTask.cancel() in your caller and in the AsyncTask use isCancelled() to tet for a cancel request.

type-a1pha
  • 1,891
  • 13
  • 19