[android] How do we use runOnUiThread in Android?

We use Worker Thread to make Apps smoother and avoid ANR's. We may need to update UI after the heavy process in worker Tread. The UI can only be updated from UI Thread. In such cases, we use Handler or runOnUiThread both have a Runnable run method that executes in UI Thread. The onClick method runs in UI thread so don't need to use runOnUiThread here.

Using Kotlin

While in Activity,

this.runOnUiThread {
      // Do stuff
}

From Fragment,

activity?.runOnUiThread {
      // Do stuff
}

Using Java,

this.runOnUiThread(new Runnable() {
     void run() {
         // Do stuff
     }
});