What is Android UiThread (UI Thread)

Can someone explain to me what UI thread is? Developer.android.com talks about runOnUiThread

public final void runOnUiThread (Runnable action)

Since: API Level 1 Runs the specified action in the user interface thread. If the current thread is a UI thread, then the action is taken immediately. If the current thread is not a user interface thread, the action is sent to the user interface thread's event queue.

Does the user interface thread mean that this will be executed every time the activity pushes the background with some ui actions, such as an incoming call or darkening the screen, etc.? If not, what exactly does the user interface thread include?

thank

+77
android ui-thread
Sep 06 2018-10-06T00:
source share
3 answers

UIThread is the main thread of execution for your application. This is where most of your application code runs. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls of these components are made in this thread.

For example, let's say your application is a single Activity class. Then, in this UIThread, all lifecycle methods and most of your event processing code are run. These are methods like onCreate , onPause , onDestroy , onClick , etc. In addition, all updates for the user interface occur here. Everything that causes the user interface to update or change must happen in the user interface thread.

Learn more about your Processes and Themes application here.

When you explicitly create a new thread to run in the background, this code does not run in UIThread. So what happens if this background thread needs to do something that changes the user interface? For this, runOnUiThread is runOnUiThread . You should actually use a handler (see the link below for more information about this). It provides these background threads with the ability to execute code that can change the user interface. They do this by putting the UI change code in the Runnable object and passing it to the runOnUiThread method.

For more information on workstation flows and user interface updates, click here.

I personally use only the runOnUiThread method in my Instrumentation tests. Since test code is not executed in UIThread, you need to use this method to run code that modifies the user interface. Therefore, I use it to enter clicks and key events in my application. Then I can check the status of the application to make sure everything happened correctly.

For more information on testing and running code in UIThread, click here.

+138
Sep 06 '10 at 18:13
source share

If you are executing lock code (for example, an Http request) in a separate thread, consider using AsyncTask . Its doInBackground Method works in a separate thread. AsyncTask provides you with the onProgressUpdate and onPostExecute , which are guaranteed to work in the user interface thread .

If you need GUI progress updates (for example, through a progress bar), call publishProgress inside doInBackground . This leads to a subsequent call to onPublishProgress , which is also guaranteed to be executed in the user interface thread .

onPostExecute automatically called after doInBackground .

+9
Feb 05 '14 at
source share

All user interface drawings, etc. occur in a separate thread. It is called UIThread. If you want to make any changes to the UI you must use, make sure that this happens in the context of UIThread. The easiest way to do this is to use runOnUiThread

+4
Sep 06 '10 at 15:48
source share



All Articles