Kotlin Handler - How do i stop Handler in Android

Published April 21, 2022

In this android example we will cover what is Handler and how to stop Handler in Android application with kotlin code.

Handler is a Thread class which will used to send and manage the Message and Runnunable objects. It will update UI from background thread by post some runnable instances.

There are 2 features used by Handler

  1. By using Handler we can schedule messages and runnables at a perticular time in the future.
  2. By using handler we can Enqueue an action perform on different thread.

 

Handler Methods

  • post(Runnable)
  • postAtTime(Runnable, long)
  • postDelayed(Runnable, Object, long)
  • sendEmptyMessage(int)
  • sendMessage(Message)
  • sendMessageAtTime(Message, long)
  • SendMessageDelayed(Message, long)

 

Create Handler in Android

We can create an Hanlder instance by using the parameterized constructor

Handler(Looper)

 

Here we need to pass Looper instance. Below we created Hanlder with MainLooper.

var handler=Handler(Looper.getMainLooper())

 

 

Schedule Message in the Future using postDelayed() method

handler.postDelayed(runnable,1000);

 

How do we cancel Handler

To cancel Hanlder callbacks we will use removeCallbacks() method.

handler.removeCallbacks(runnable)

 

Let create simple example to close handler on Activity finish

 

package com.rrtutors.kotlinhandler


import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class HandlerActivity : AppCompatActivity() {
    var runnable= Runnable {
        Toast.makeText(baseContext,"Handler Running ",Toast.LENGTH_SHORT).show()
    }
    var handler=Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_handler)
    }

    fun start(view: android.view.View) {
        handler.postDelayed(runnable,1000);
    }
    fun stop(view: android.view.View) {

        handler.removeCallbacks(runnable)
    }
}

 

Xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HandlerActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:onClick="start"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="100dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        android:onClick="stop"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="100dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

 

Android Handler Close/cancel with kotlin

 

Conclusion: In this android example we covered how to close the Handler with kotlin code

 

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1719 Views