Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 474
Harald Wilhelm Avatar asked May 21 '12 13:05

Harald Wilhelm


1 Answers

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.

like image 117
waqaslam Avatar answered Oct 26 '22 12:10

waqaslam