0

RunOnUIThread feels like the UI thread in an Android application is just the UI thread, Is the same thread that's used for all activities throughout a single application?

If so , do I must use Activity.RunOnUIThread? Or Is there any alternative?

Leon
  • 8,404
  • 2
  • 9
  • 52

3 Answers3

1

RunOnUiThread runs code on UI thread (surprise!). if the current thread is UI thread, that code will be executed immediately, otherwise (if the current thread is not the UI thread), the code will be queued in UI thread's event queue.

UI thread is the main thread of execution and most of codes run on it. (actually any code that change UI should run on UI thread)

Do I must use Activity.RunOnUIThread?

You have to use RunOnUIThread when you are in Non-UI thread (e.g: background thread).

Or Is there any alternative?

If you mean is there any other method that force code to run on UI thread, yes, I think you can use Handler for that. (this may helps: Why to use Handlers while runOnUiThread does the same?)

Bonus: Here is my 2 helper methods that related to the question:

public bool IsMainThread 
    => Build.VERSION.SdkInt >= BuildVersionCodes.M
        ? Looper.MainLooper.IsCurrentThread
        : Looper.MyLooper() == Looper.MainLooper;

public void RunOnMainThread(Action action)
{
    if (IsMainThread) action();
    else RunOnUiThread(action);
}
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
0

The runonUIthread is used when you run a command (such as a snackbar as an example) from a worker thread. Anything that affects the display should be on the same thread to prevent weird artifacts. And just in case you didn't know, android is multi-threaded. It uses one for UI and if you program it properly, any processing you do is on a different thread. That way your interface always acts butter-smooth.

John Lord
  • 1,941
  • 12
  • 27
0

Is the same thread that's used for all activities throughout a single application?

Threading in Android is simpler then you might think, any application, in general, can have n number of threads, but there is only one UI Thread. The thread on which all UI related changes are made is what is called the UI thread. With RunOnUIThread If the current thread is UI thread, that code will be executed immediately, otherwise (if the current thread is not the UI thread), the code will be queued in UI thread's event queue. In case if you make any changes in your UI and if it is in some background thread like an async-await based Restful Apicall then it won't get reflected as that was not made on the Background thread and hence id not known to the running 'Activity', since RunOnUIThread is only available in an activity context in fragments you will need to use the Activity property to get it and use it.

If so , do I must use Activity.RunOnUIThread? Or Is there any alternative?

It is not always needed, but there are scenarios where you might wanna use it. (As explained above)

For eg: An async method that opens a popup after its done executing will need the RunOnUIThread method otherwise your Pop-Up or alert will never show up because the change was not made on UIThread but an asynchronous background thread.

Similarly, If you have an OnCreate method and you have a requirement to show an Alert as soon as the page opens now if you decide to write this down directly in the OnCreate method there are chances it might not show in that case you run it on UIThread.()

FreakyAli
  • 13,349
  • 3
  • 23
  • 63