In Kotlin, is there a way to refer to a listener instance when using this short notation for anonymous classes? In this case, this refers to an external context (for example, an Activity instance), where view is defined:
view.setOnClickListener { val self: View.OnClickListener = this
When using a longer notation in which you explicitly specify the interface to be implemented, and where you explicitly override the callback method, the listener can be referenced via this :
view.setOnClickListener(object: View.OnClickListener { override fun onClick(v: View) { val self: View.OnClickListener = this
source share