onPostExecute not called after completion AsyncTask

Viewed 43909

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

8 Answers

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!

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.

Related