https://kotlinlang.org logo
#android
Title
# android
c

cubesky

05/29/2017, 7:26 AM
Hello, I have a problem when I use Kotlin in Android. I create a handler using
Copy code
kotlin
val handler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                0 -> { fab_next.show() }
            }
            super.handleMessage(msg)
        }
    }
and Android Studio told me
This Handler class should be static or leaks might occur
,but kotlin doesn't have
static
,So what should I do for this?
l

louiscad

05/29/2017, 7:28 AM
cubesky: You don't need to use the static keyword. In Kotlin, to get the same effect as a non static class, you use
inner class
. In your case, you have to use a non anonyous class and use a WeakReference
This delegate can help you for WeakReference: https://gist.github.com/LouisCAD/7ce3a30cb88c27858b12d5f26c118b0b
👍 1
6 Views