ddavtian
ddavtian

Reputation: 1361

AsyncTask onPostExecute never gets called

I am not sure what I am doing wrong but onPostExecute never gets called.

The onPreExecute(), doInBackground(..) gets triggered, but for some reason the onPostExecute never gets called.

Thank you in Advance!

Dave

 private class PostToOpenFeint extends AsyncTask<Void, Void, Void> {
  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  @Override
  protected Void doInBackground(Void... params) {
   // does all the work here
   return null;
  }


  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
   */
  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   super.onPostExecute(result);
   Toast.makeText(MainScreen.this, "Done syncing", Toast.LENGTH_LONG).show();
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onPreExecute()
   */
  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   super.onPreExecute();
   Toast.makeText(MainScreen.this, "About to sync all your scores", Toast.LENGTH_LONG).show();
  }

Looking into it more, this is what I was able to observe. For example if I place this call:

 new PostToOpenFeint.execute();

right after onCreate of the Activity, then everything works fine. If I place this call say inside a button listener.

settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
   new PostToOpenFeint.execute();
}
});

The onPostExecute() never gets called, not sure what I am doing wrong. The restriction I read was to call this from UI Thread and I am calling it from UI thread.

Upvotes: 40

Views: 54745

Answers (11)

CoolMind
CoolMind

Reputation: 28793

In my case I called downloading a file in UI thread. Then refreshed a token in IO and tried to download a file in UI. Refreshing a token called AsyncTask (TokenRequestTask) and didn't call onPostExecute.

Because everything was made with coroutines, I didn't know where is a mistake. Then downloaded a file in Dispatchers.IO, and the problem disappeared.

Upvotes: 0

Uttkarsh Rastogi
Uttkarsh Rastogi

Reputation: 1

I was also facing the same problem what is was doing wrong is that I was trying to write the method in a different scope the method will only trigger when you write it after the doInBackground method and inside the PostToOpenFeint class (in this case)

Upvotes: 0

Yasser AKBBACH
Yasser AKBBACH

Reputation: 1028

Just return something rather than null, and it'll work!
eg:
return "done";

Upvotes: 1

vinaya
vinaya

Reputation: 1

Just use the proper Parameters in onPostExecute, use public void onPostExecute(Boolean response) { } instead of public void onPostExecute(boolean response) { } this solved my problem

Upvotes: 0

Tom E.
Tom E.

Reputation: 754

I had a similiar problem just now. I was extending AsyncTask<Integer,Integer,Integer> and my onPostExecute method looked like:

protected void onPostExecute(int result)

This seemed OK to me, but then again I'm more of a .NET guy than a Java guy. I changed it to:

protected void onPostExecute(Integer result)

because for some reason Java thinks there is a difference between int and Integer?

I think that your problem is that you're declaring the return type as void in your:

extends<Void,Void,Void>

That means no params, no progress and no result. If you want to execute something when it's done, I think you need to give it some kind of value like and integer or a boolean.

If you change your extends to:

<Void,Void,Integer>

Now your passing no params, not publishing any progress, but are returning a value. Change your onPostExecute method to accept an Integer:

protected void onPostExecute(Integer result)

Then change the return in your doInBackground method to something like:

return 1;

Then it should execute for you.

Upvotes: 73

Michael Kronberger
Michael Kronberger

Reputation: 143

Just change the parameters in onPostExecute

@Override
protected void onPostExecute(Void unused) {
...}

Now the method should be called.

Upvotes: 1

Christian Mock
Christian Mock

Reputation: 131

There's one more gotcha about AsyncTasks that I just spent half the day on:

The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.

Says the documentation. On Gingerbread (and probably before), given the right circumstances, this leads to onPostExecute not being run. The solution, however, is easy, just a

new MyAsyncTask();

early in the UI thread so the class gets loaded before anything in the background can inadvertently load it.

I had used a static function in my AsyncTask subclass from a background thread that, depending on timing, got called before anything on the UI thread used that AsyncTask subclass, and bingo, a nice Heisenbug.

(Bear with me for answering this old, already answered question, but I didn't find that tidbit of info anywhere, and this is my first post)

Upvotes: 13

Rajesh Narwal
Rajesh Narwal

Reputation: 908

Just Add this to Code to your Activity. Its the problem of Async Task not initializing properly as it is supposed to do.

          try {
                Class.forName("android.os.AsyncTask");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Upvotes: 1

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

Note that if you cancel the task via cancel(), onPostExecute() won't be called. Pretty logical, but simply overlooking a call to that method had me stumped.

Upvotes: 9

Oz Radiano
Oz Radiano

Reputation: 789

My problem was that somehow I didn't @Override the onPostExecute method correctly. I fixed that by clicking right mouse button->source-> Override\Implement method.

Hopes it helps someone

Upvotes: 7

Kevin Gaudin
Kevin Gaudin

Reputation: 9945

Maybe the code in doInBackground() never returns?

Upvotes: 2

Related Questions