KOTLIN TIP #3

LAMBDAS

Lambdas reduce the overall lines of code in a source file and allow for functional programming. While lambdas are currently possible with Android, Kotlin takes them a step further by ensuring you don’t have to deal with Retrolambda or changing the way your build is configured.

For example, an on-click listener would look like:

button.setOnClickListener { view ->
startDetailActivity()
}

It even works with return values:

toolbar.setOnLongClickListener {
showContextMenu()
true
}

The Android SDK contains plenty of cases where you are setting a listener or implementing a single method. Lambdas work great in those circumstances.

Leave a comment