38

For some reason my onPostExecute() is not called after my AsyncTask finishes.

My class decleration:

public class setWallpaperForeground extends AsyncTask<String, Integer, Boolean>

My onPostExecute():

protected void onPostExecute(Boolean result)

Everything works fine, my doInBackground() completes successfully and returns a Boolean but then it just finishes.

Thanks

mlevit
  • 2,676
  • 10
  • 42
  • 50

8 Answers8

56

Did you start the task with execute() method? The onPostExecute wouldn't run if you just invoke the doInBackground.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
48

Did you create your AsyncTask on the UI thread? Also add an @Override annotaiton on your onPostExecute() method to make sure you declared it correctly.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 1
    I didn't have the @Override there. Looking at the example (http://developer.android.com/reference/android/os/AsyncTask.html) neither do they. Why must I have one over onPostExecute() but none of the others? – mlevit Aug 31 '10 at 08:19
  • 1
    It seems to work on the Emulator but not on the phone. When the background processes finish no Toast notification appears. The layout does look like it re-loads for some unknown reason. Any ideas? – mlevit Aug 31 '10 at 13:09
  • 8
    You don't have to have @Override, but it's a way to ensure you properly overrode the method. – Romain Guy Aug 31 '10 at 16:44
  • 1
    I had made the mistake of using OnPostExecute instead of 'onPostExecute'.Thanks for the tip :) – rogerstone Jun 03 '11 at 12:31
  • I had the Asynctask created outside of the UI thread. Thanks for the solve! – Radu Jan 18 '13 at 10:44
  • I get an error when overriding the onPostExecute... anyone know why? – Darrell Jul 23 '14 at 13:57
31

Found/Made another nasty mistake:

If your params of onPostExecute(Param param) don't match the one you defined with extends AsyncTask<...,...,Param> and you didn't use the @Override annotation, it will never be executed and you don't get a warning from Eclipse.

Note to myself: Just always use the @Override annotation and Eclipse will help you.

Another easy way to avoid all named mistakes:

in Eclipse: Right-click in code > Source > Override/Implement Methods

trichner
  • 840
  • 1
  • 11
  • 22
  • 2
    I just solved my colleague's problem here - he was using boolean instead of Boolean! – PayToPwn May 08 '17 at 11:49
  • Wow, thank you. I had no idea prior to this post that when I extend AsyncTask I have to explicitly mark the type of the returned value. I just had `extends AsyncTask` with no types – TKoL Jun 13 '22 at 15:09
5

After having the same problem and none of these answers helped me, I found out that my UI thread was blocked (I used a CountDownLatch.await()) and therefore the onPostExecute() method that is supposed to be called by the UI thread was never called.

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
Hadas Kaminsky
  • 1,285
  • 1
  • 16
  • 39
3

Made another nasty mistake that can result in this same error. When defining the AsyncTask and calling it, I was not calling execute but was calling doInBackground

new AsyncTask<String,Void,Void>() {
    ....
}.doInBackground("parameter");

rather than

new AsyncTask<String,Void,Void>() {
    ....
}.execute("parameter");
Anarchofascist
  • 474
  • 5
  • 15
2

I have faced the same problem. None of the above solutions worked for me. Then i figured out the problem maybe it helps someone else .

In UI thread i call the following codes:

public class XActivity ...{
    onCreate(){
        ....

        new SaveDrawingAsync(this).execute();

        while(true)
        {
            if(MandalaActivity.saveOperationInProgress){
                continue;
            }
            super.onBackPressed();
            break;
        }

        ...
    }
}

My AsyncTask class definition :

public class SaveAsync extends AsyncTask<Object, Void, Void> {

    @Override
    public Void doInBackground(Object... params) {
        saveThem(); // long running operation
        return null;
    }

    @Override
    public void onPostExecute(Void param) {
        XActivity.saveOperationInProgress = false;
    }

    @Override
    public void onPreExecute() {
       XActivity.saveOperationInProgress = true;
    }

}

in the above code onPostExecute is not called. It is because of an infinite loop after asynctask execution . asynctask and inifinite loop both waits eachother to finish. Thus the code stucks!

The solution is changing the design!

oiyio
  • 5,219
  • 4
  • 42
  • 54
1

I had the same behaviour, and the cause was that I have been posting a lot of messages as a progress inside doInBackground with following code:

new Handler(Looper.getMainLooper()).post(new Runnable() {
   @Override
   public void run() {
     // .. some UI updates
   }
});

this must have overloaded main thrad message queue, and caused long delay before onPostExecute would get called. The solution was to post only once every second.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I counted number of seconds (using System.currentTimeMillis()) between each time I posted to handler – marcinj Feb 22 '16 at 13:31
0

For me it was user error. I was ending the AsyncTask by invoking cancel(true) on it and not reading the documentation closely enough to know that onPostExecute is not called in this case, onCancelled is.

AlanKley
  • 4,592
  • 8
  • 40
  • 53