Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How to pass a Runnable as this in Handler

I'm beginner in kotlin. I try to create a task that will repeat every 2 seconds. So I created something like this.

val handler = Handler()
    handler.postDelayed(Runnable {
        // TODO - Here is my logic

        // Repeat again after 2 seconds
        handler.postDelayed(this, 2000)
    }, 2000)

But in postDelayed(this) it gives error - required Runnable!, found MainActivity. I've tried even this@Runnable but it didn't work.

But when I write the same function like this, it works

val handler = Handler()
    handler.postDelayed(object : Runnable {
        override fun run() {
            // TODO - Here is my logic

            // Repeat again after 2 seconds
            handler.postDelayed(this, 2000)
        }
    }, 2000)

So why the this keyword doesn't work in first function, but in second function it works good?

like image 697
Hayk Mkrtchyan Avatar asked Mar 19 '20 16:03

Hayk Mkrtchyan


2 Answers

You have several options to go about here:

  1. make both the runnable and the handler be in the same scope

        //class scope
        val handler = Handler()
        val runnable = object : Runnable {
           override fun run () {
             handler.removeCallbacksAndMessages(null) 
             //make sure you cancel the 
              previous task in case you scheduled one that has not run yet
             //do your thing
    
             handler.postDelayed(runnable,time)
          }
       }
    

then in some function

handler.postDelayed(runnable,time)
  1. You can run a timertask, which would be better in this case

     val task = TimerTask {
        override fun run() {
         //do your thing
        }
     }
    
     val timer = Timer()
    
     timer.scheduleAtFixedRate(task,0L, timeBetweenTasks)
    
like image 163
Lena Bru Avatar answered Oct 24 '22 00:10

Lena Bru


The first one is a function that accepts a lambda and returns a Runnable. In this case this means nothing.

The second one you're defining an anonymous object that implements Runnable. In this case this refers to that object instance.

like image 3
m0skit0 Avatar answered Oct 23 '22 23:10

m0skit0