40

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

  • Created a base class called BaseActivity.java
  • From my original Activity I extended this class.
  • Placed PostToOpenFeint class inside BaseActivity
  • Called it from the UI thread from the main activity my doing:

    new PostToOpenFeint.execute();
    

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.

ddavtian
  • 1,361
  • 3
  • 14
  • 16
  • Please fix your post title: ActiveTask should be replaced with AsyncTask – Kevin Gaudin Dec 20 '10 at 22:57
  • Ahh thank you, seems like someone beat me to it. – ddavtian Dec 21 '10 at 04:37
  • Did you ever solve this issue? I have the same problem right now. Same behavior when called from onClickListener of a button. – Bashorings Feb 01 '12 at 11:45
  • 2
    Nevermind, instead of calling execute() on the task (like I do for all my other tasks), I called the doInBackground() directly...I don't know how that happened. :-P – Bashorings Feb 01 '12 at 12:30
  • @Bashorings It isn't a good idea to call doInBackground directly it defeats the purpose of an asynchronous task to call all of its methods synchronously from the UI thread – kiwijus Apr 17 '12 at 12:38

11 Answers11

73

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Tom E.
  • 754
  • 5
  • 2
  • 11
    +1 for:"java thinks theres a difference between int and Integer" – Zonata Jul 23 '12 at 15:19
  • 20
    There is a difference between int and Integer. Integer is an object, can be null, int is a primitive, cannot be null. It's particularly important when the function you call returns an Integer and you are mystified by a null pointer exception when you try to store it to an int. – f20k Aug 25 '12 at 14:25
  • 3
    Just a hint, or recommendation: You can actually leverage the compiler to detect such issues by tagging overridden methods with the @Override annotation. In the case of onPostExecute(int result) it would have shown an error, indicating that there is no such super method with an int parameter. By the way, Void result values work just as well. – Michael Jess Apr 10 '13 at 20:17
  • Also in Xamarin, if you want to pass custom objects or C# objects, make sure to extend those objects to `Java.Lang.Object` – Pierre Nov 14 '18 at 08:23
13

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)

Christian Mock
  • 131
  • 1
  • 3
  • Thanks for talking about UI thread. In my case it helped to find a mistake in coroutine dispatcher. – CoolMind Nov 02 '22 at 11:56
9

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.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
7

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

Oz Radiano
  • 779
  • 2
  • 11
  • 30
2

Maybe the code in doInBackground() never returns?

Kevin Gaudin
  • 9,927
  • 3
  • 32
  • 34
  • 2
    I am having this problem and am 100% sure I am returning a valid Bitmap at the end of doInBackground() call, but it's in the GL thread if that makes any difference? – HaMMeReD Dec 21 '10 at 05:51
1

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();
            }
Rajesh Narwal
  • 898
  • 8
  • 8
1

Just change the parameters in onPostExecute

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

Now the method should be called.

Michael Kronberger
  • 93
  • 1
  • 2
  • 12
1

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

Yasser AKBBACH
  • 539
  • 5
  • 7
0

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

vinaya
  • 1
  • I think It should not be `public void onPostExecute` , it should be `protected void onPostExecute` . – kAmol Sep 04 '18 at 06:02
0

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)

AgentP
  • 6,261
  • 2
  • 31
  • 52
0

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.

CoolMind
  • 26,736
  • 15
  • 188
  • 224