Harald Wilhelm
Harald Wilhelm

Reputation: 6716

What does "runs on UI thread" for onPostExecute() really mean?

Consider an AsyncTask started in an Activity. What happens if the Activity is paused or destroyed? Will onPostExecute() run? If yes, what UI thread will be used?

Just wondering.

Many thanks in advance.

Upvotes: 12

Views: 9571

Answers (3)

Tai Tran
Tai Tran

Reputation: 1404

It will crash in onPostExecute() because of UI thread like a process Dialog. It happens to me, my Activity destroy before my Asynctask completes. It says "window leaked....." So, I remove onPostExecute(). No more crash..

Upvotes: 1

beiliubei
beiliubei

Reputation: 183

Remember to call cancel function to be void of exception in you onPause or onDestroyed.

Upvotes: 0

waqaslam
waqaslam

Reputation: 68187

UI thread is available throughout the visible life of your application which may span on a combination of multiple activities.

Anything you change in views must be performed on UI thread and onPostExecute of AsyncTask reflects the same logic by executing the instructions inside on UI thread.

You may use runOnUiThread in your own Thread to make changes on Views. But since AsyncTask has onPostExecute method (which also runs on UI thread) so you dont logically need to use runOnUiThread there.


Update

Regarding your question: Yes, onPostExecute will still be called (because its invoked by a separate thread) even if your activity is destroyed and if the method will manipulate Views, you'll simply get Force Close error because reference to your activity is not available anymore.

Upvotes: 12

Related Questions