Lloyd Dcosta
Lloyd Dcosta

Reputation: 301

Why is it possible to update UI using Handler created in a background Thread?

I have a worker class which has a Handler created in it. When I try to update my UI using the handler created in background thread its updating the UI and there is no crash here. Ideally it should crash as its running in the background thread. Please find the code below:

class WorkerThread(val mainThreadHandler: Handler, handlerThreadName: String) :
  HandlerThread(handlerThreadName) {

  lateinit var workerThreadHandler: Handler

override fun onLooperPrepared() {
    super.onLooperPrepared()
    Log.d("LLoyd", "onLooperPrepared")


}


override fun run() {
    super.run()
    Log.d("LLoyd", "run method called")

}

fun startTask(){
    workerThreadHandler = object : Handler(this.looper) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            Log.d(
                "Lloyd",
                "Message from Main Thread received " + msg.what + "  Thread name  " + 
currentThread().name
            )
            val message = Message()
            message.what = msg.what
            mainThreadHandler.sendMessage(message)
        }
    }

 }
}

This startTask() is called from activity on click of a button as below:

val workerThread = WorkerThread(mainThreadHandler, "Lloyd")
    workerThread.start()
    btn_task1.setOnClickListener {
        workerThread.startTask()
        val message = Message()
        message.what = FROM_MAINTHREAD
        workerThread.workerThreadHandler.sendMessage(message)
        val message1 = Message()
        message.what = 55
        Log.d("Lloyd ", "Outside Thread name " + Thread.currentThread().name)

        workerThread.workerThreadHandler.post {
            Log.d("Lloyd ", "Runnable Thread name " + currentThread().name)
            btn_task1.text = "Task 2"
            tv.text= "YAAAAAAAAAAAAAAAAAAAAAAy"
        }
    }

Any kind of help is Appreciated!

Upvotes: 1

Views: 696

Answers (2)

Khamidjon Khamidov
Khamidjon Khamidov

Reputation: 8899

This was a problem for background tasks before. But Handler class is specifically create for updating UI in background tasks as Handler is in UI thread itself. For example, when you accomplish heavy task and need to update the ui you can do

new Thread(new Runnable() {
         @Override
         public void run() {

               // here you perform background operation
               //Update the value background thread to UI thread
               mHandler.post(new Runnable() {
                  @Override
                  public void run() {
                     // here you can update ui
                  }
               });
            }
         }
      }).start();

for more information you can also learn here

Upvotes: 0

a_local_nobody
a_local_nobody

Reputation: 8191

When you connect a Handler to your UI thread, the code that handles messages runs on the UI thread.

taken from the docs

i believe because you're calling the workerThread.startTask() from the button click, which is the UI thread, the handler is simply returning the result to the UI thread as well, which is why the UI will update as well

Upvotes: 2

Related Questions