There are two types of thread in Android.
1 is UI or Main thread on which your UI elements (layouts) are rendered.
2 is Worker Thread in which long task should be executed (like AsyncTask & Networking).
If you write some task in new Thread
, that mean that task will be executed in worker thread.
Now you will use runOnUiThread
or new Handler(Looper.getMainLooper())
because you can not touch UI elements in worker thread.
So basically when you are updating UI like setText()
, or Toast
or any UI operations, you will have to UI thread and you should use worker thread when you are doing some long executions.
Edit
Generally we don't have to manage threading in Android. Because all libraries we use are smart. Although in some cases we have to manage threading as well.
Example
Assume you are calling an web-service(api) in a new Thread
, now when response comes you want show a Toast
. If you just write Toast.show...
directly in response inside worker Thread
you will get exception.
Only the original thread that created a view hierarchy can touch its views.
Now to overcome this issue you have to use runOnUiThread
, so that you can show Toast
.