2

I aim to avoid AsyncTask's onPostExecute() being called. cancel() can't help me, because it doesnt work in 100% cases: sometimes cancel() returns false and later onPostExecute() of the task is called. How to prevent this? I created boolean flag in my class and set it to true instead of invoking cancel(), but it is hardly a good solution:

public class GetIcons extends AsyncTask<Void, Void, HashMap<String, Bitmap>>
{
  public boolean skipPostExecute = false;

  @Override
  protected HashMap<String, Bitmap> doInBackground(Void... arg0)
  {
    // do something (getting icons from webserver)
  }

  @Override
  protected void onPostExecute(HashMap<String, Bitmap> result)
  {
    super.onPostExecute(result);
    if (!skipPostExecute)
    {
       // do something
    }
  }

}

Is there a better way to do the same?

Eugene Chumak
  • 3,272
  • 7
  • 34
  • 52
  • see this answer http://stackoverflow.com/a/10882600/1289716 – MAC Jun 26 '12 at 13:10
  • Also refer to discussion here: http://stackoverflow.com/q/10163763/165674 – Dheeraj Vepakomma Jun 26 '12 at 13:15
  • Under what cases does `onPostExecute()` get called even after a call to `cancel()`? – Dheeraj Vepakomma Jun 26 '12 at 13:17
  • Hey do not override `onPostExecute()` wont it solve your problem?? – Raghav Jun 26 '12 at 13:20
  • to Dheeraj: onPostExecute() gets called after a call to cancel(), when cancel() returns false. to iNan: if I dont override onPostExecute(), its superclass implementation will still be called, because my asynctask is not cancelled. Besides, I need to do some things in onPostExecute, so I have to override it. – Eugene Chumak Jun 26 '12 at 15:17
  • override your onCancelled method in your asynctask class. so onPostExecute() wont be called after asynctask.cancel(true/false). – VijayDroid Jan 14 '15 at 09:54

0 Answers0