What is the Android UiThread (UI thread)
Asked Answered
E

3

90

Can someone explain to me what exactly the UI thread is? On developer.android.com it says about the runOnUiThread function

public final void runOnUiThread (Runnable action)

Since: API Level 1 Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

Does the UI thread mean that this will be run everytime the activity is pushed the the background by some ui activity like incoming call or screen dimming etc.? If not, what exactly does the UI thread include ?

Thank you

Eterne answered 6/9, 2010 at 15:20 Comment(0)
A
150

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

For instance, let's say your application is a single Activity class. Then all of the lifecycle methods and most of your event handling code is run in this UIThread. These are methods like onCreate, onPause, onDestroy, onClick, etc. Additionally, this is where all of the updates to the UI are made. Anything that causes the UI to be updated or changed HAS to happen on the UI thread.

For more info on your application's Processes and Threads click here.

When you explicitly spawn a new thread to do work in the background, this code is not run on the UIThread. So what happens if this background thread needs to do something that changes the UI? This is what the runOnUiThread is for. Actually you're supposed to use a Handler (see the link below for more info on this). It provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the runOnUiThread method.

For more info on spawning worker threads and updating the UI from them click here

I personally only use the runOnUiThread method in my Instrumentation Tests. Since the test code does not execute in the UIThread, you need to use this method to run code that modifies the UI. So, I use it to inject click and key events into my application. I can then check the state of the application to make sure the correct things happened.

For more info on testing and running code on the UIThread click here

Andromache answered 6/9, 2010 at 18:13 Comment(3)
Great explanation , specially link for app fundamentals its a must read for all beginners like me :)Copywriter
Shouldn't you use an AsyncTask instead of runOnUiThread in most cases?Landfall
@Landfall the 2 techniques have different purposes: AsyncTask will get you OFF the main thread, into the background; runOnUiThread() will get you ONTO the main thread, from the background. So it depends what you are trying to accomplish.Succinctorium
C
10

If you execute blocking code (e.g. a Http-Request) in a separate Thread, consider using AsyncTask. Its doInBackground-Method runs on a separate Thread. AsyncTask provides you with methods onProgressUpdate and onPostExecute which are guaranteed to run on the UI thread.

If you need GUI-progress updates (e.g. via a progressbar) call publishProgress inside doInBackground. This leads to a subsequent call of onPublishProgress which is also guaranteed to run on the UI thread.

onPostExecute is automatically called after doInBackground returns.

Cephalic answered 5/2, 2014 at 13:57 Comment(0)
Z
5

All UI drawings etc. happen in a separate thread. Its called the UIThread. If you want to make any change to UI u must use make sure it happens in UIThread's context. Easiest way of doing it is to make use of runOnUiThread

Zelig answered 6/9, 2010 at 15:48 Comment(2)
Can you prepare widget data structures (like inflate a Layout) in a non UI thread and then draw them later in a UI thread?Uranology
never tried that but technically it should be possible, since inflate just creates the data structure. Its the part when u attach is to an activity etc is when u need to do it in a UIThread context.Zelig

© 2022 - 2024 — McMap. All rights reserved.