Does onPreExecute() and onPostExecute() execute on the UI thread or on the thread from which the AsyncTask has been started? Does onPreExecute() and onPostExecute() execute on the UI thread or on the thread from which the AsyncTask has been started? multithreading multithreading

Does onPreExecute() and onPostExecute() execute on the UI thread or on the thread from which the AsyncTask has been started?


While docs say that these callbacks execute in main thread - it is not true. onPreExecute() runs synchronously from the executeOnExecutor() i.e. in the thread that starts AsyncTask.

onPostExecute() is always runs in main thread. (it is invoked from finish(), and this happens inside Handler that uses looper of main thread).


You should do UI updates and alert or pop up showing in AsyncTask's onPostExecute() method which runs on UI thread. The AsyncTask's doinBackground() method runs on another thread.


Quoting offical Docs:

onPostExecuteAdded in API level 3void onPostExecute (Result result)Runs on the UI thread after doInBackground(Params...). The specified result is the value returned by doInBackground(Params...).onPreExecuteAdded in API level 3void onPreExecute ()Runs on the UI thread before doInBackground(Params...).

you can find it here https://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result)

Do in background runs on background thread while OnPreExecute and OnPostExecute run on main Ui thread.