0

I have an EditText and an AsyncTask class. Whenever the text changes in the EditText, the AsyncTask class's object created and called execute() on it with the string. And the doBackground() is called which has only a single function which in turn has multiple for loops one after another. A new object for AsyncTask is created whenever, the text changes. When the text changes, the old Async object has to be cancelled and the new one should take control. But when I call cancel on the object, the old one continues to run and seems to call onPostExecute(). Is there a way to avoid this. I mean, can I kill/(stop running) the old asyncTask object completely before running the new one?

EDIT This is my code,

if (getSuggestions1.getStatus() != AsyncTask.Status.RUNNING) {
    getSuggestions1.cancel(true);
    getSuggestions1 = new GetSuggestions1();
    getSuggestions1.execute(new String[] { str });
}

As you can see I am calling execute() immediately after calling cancel on the same object. Also, as I said, I have multiple for loops(6). I am worried the function, the isCancelled(), returns true for one of the for loops as the object initialized immediately.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
rahul
  • 6,447
  • 3
  • 31
  • 42
  • See [THIS](http://stackoverflow.com/a/10882600/1289716) answer that may help you... – MAC Apr 13 '13 at 20:47
  • 1
    "As you can see I am calling execute() immediately after calling cancel on the same object." They are NOT the same objects. You're calling it in the same pointer that after the 'new' keyword it is pointing to a new object. – Budius Apr 13 '13 at 21:01
  • See **Cancelling a Task** on [this Android API doc page](http://developer.android.com/reference/android/os/AsyncTask.html) – Nate May 29 '13 at 21:19

3 Answers3

2

In addition to the already given answers.

Stopping (cancelling) an AsyncTask is a cooperative action. This means that simply calling AsyncTask.cancel() is not enough. This call just sets a cancelled status (a flag) and does nothing more. Your AsyncTask implementation should provide a cooperation (help) in order to actually stop processing. Specifically this means that if you want your AsyncTask's cancellability to be highly responsive you need to arrange checking for cancelled status rather frequently. So put if (isCancelled()) { return null; } statements inside of your loops.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
1

Try to use something like

while (!isCancelled()) {
    // processing
}

or

if (!isCancelled()) {
    // processing
}

The if-sentence is good if you're having many loops, at least that's what I've experienced. These should be used in doInBackground() of course.

gosr
  • 4,593
  • 9
  • 46
  • 82
0

You might create a boolean flag called shouldCancel in your class extending ASyncTask and periodically check it in the doInBackground(), if it's set, then return from doInBackground method.

Axarydax
  • 16,353
  • 21
  • 92
  • 151
  • 1
    AsyncTask already has [isCancelled()](http://developer.android.com/reference/android/os/AsyncTask.html#isCancelled()) exactly for this purpose. No need to create another flag. – Nate May 29 '13 at 21:17