Are onPreExecute () and onPostExecute () executed in the user interface thread or in the thread from which AsyncTask is launched?

I wrote AsyncTask for short background operations in android for quite a while and had a very simple question. If I started AsyncTask from a separate thread, and not from the main UI thread, my onPreExecute() and onPostExecute will still be called in the UI thread or the thread that I started AsyncTask . I'm curious because I was not able to show the popup inside the onPreExecute() method when I started it from some other thread.

EDIT 2

 I tried writing this simple activity to try: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { final TestAsyncTask task = new TestAsyncTask(); task.execute(); } }).start(); } private class TestAsyncTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... voids) { return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); Toast.makeText(MainActivity.this, "Yo!", Toast.LENGTH_LONG).show(); } } } 

This is normal. But when I run the application with the following code:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { final TestAsyncTask task = new TestAsyncTask(); task.execute(); } }).start(); } private class TestAsyncTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); Toast.makeText(MainActivity.this, "Yo!", Toast.LENGTH_LONG).show(); } @Override protected Void doInBackground(Void... voids) { return null; } } } 

The following error failed:

 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

as one of the lines in the stack trace.

+5
source share
3 answers

While the docs say that these callbacks are executed in the main thread - this is not true. onPreExecute() is executed synchronously with executeOnExecutor() i.e. in a thread starting with AsyncTask .

onPostExecute() always starts in the main thread. (it is called from finish() , and this happens inside the Handler , which uses the looper of the main thread).

+7
source

You should do UI updates, and also show or pop up in AsyncTask onPostExecute (), which runs in the UI thread. The AsyncTask doinBackground () method works in another thread.

+4
source

Assignment of official documents:

 onPostExecute Added in API level 3 void onPostExecute (Result result) Runs on the UI thread after doInBackground(Params...). The specified result is the value returned by doInBackground(Params...). onPreExecute Added in API level 3 void 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)

Make a background thread in the background while OnPreExecute and OnPostExecute are running on the main Ui branch.

0
source

All Articles