The difference between the OnclickListener methods in Kotlin

I study Kotlin. Before that, I worked with Java for Android Development. Kotlin is a great language to learn. I got confused when I used setOnClickListener(View.OnClickListener) . I saw two hints of Android Studio.

image

I know how to work or define both of them.

The first way to implement OnClickListerner

  send_button.setOnClickListener(object : View.OnClickListener{ override fun onClick(p0: View?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }) 

and this is the second way to implement OnClickListener

  send_button.setOnClickListener { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } 

I understand how the second method is based on lambda. But I cannot correctly understand these methods.

So my question is: what is the difference between these methods? If they are different, which one is better and why?

+7
android kotlin
source share
2 answers

They refer to the same function, but are written differently. The first is a typical way to call the setOnClickListener() function, which takes an OnClickListener object.

The second uses a lambda expression. This works because View.OnClickListener is a SAM type defined in Java, and Kotlin supports SAM Conversions .

Like Java 8, Kotlin supports SAM conversions. This means that Kotlin function literals can be automatically converted to implementations of Java interfaces using one method other than the default if the parameter types of the interface method correspond to the parameter types of the Kotlin function.

Suppose you have an interface defined in Java, for example:

 public class Example { public interface SingleMethodInterface { int length(String text); } public static void set(SingleMethodInterface sam) {} } 

You can then call the set() function in two ways in Kotlin:

 //Passing a function type which accept a `String` and return an `Int` val length: (String) -> Int = { it.length } Example.set(length) //or Example.set { it.length } //Passing an object which implements `SingleMethodInterface` val length2: Main.SingleMethodInterface = object: Main.SingleMethodInterface { override fun length(text: String): Int { return text.length } } Example.set(length2) 

In short, SAM conversion provides a way to write clean code in Kotlin that interacts with Java.

+2
source share

Your question: What is the difference between these methods? If they are different, which one is better and why?

After compilation, both will generate the same bytecode, but when writing this will give you more readability with a single-line implementation.

A great feature of Java 8 are Lambda expressions. With Lambda, you can write java code easier. The ugly look of the code has slightly changed with this function.

Example: Source

You can write Single Abstract Method (SAM) as lambda.

If your interface has only one method.

 public interface StateChangeListener { public void onStateChange(State oldState, State newState); } 

You can write it like a lambda, like this.

 stateOwner.addStateListener( (oldState, newState) -> System.out.println("State changed") ); 

But both methods are the same, but you can see that the second one is so simple and remote from the ugly implementation.

In Kotlin, lambda expressions are different from Java.

Kotlin lambda expression example : source

 val add: (Int, Int) -> Int = { a, b -> a + b } 

The above function variable can be called as follows:

 val addedValue: Int = add(5, 5) 

This will return you the added value of two integers.

In this example, you can see (Int, Int) -> Int , this is called the lambda function in Kotlin.

So the lambda functions lambda lambda and lambda Java are completely different.

You can see in the Kotlin Docs:

Like Java 8, Kotlin supports SAM conversions. This means that Kotlin functional literals can be automatically converted to implement Java interfaces using one method other than the default, as long as the parameter types of the interface method match the parameter types of the Kotlin function.

Actually, you write lambda in kotlin, later it will be converted to a java interface. Thus, both methods are different. But when it comes to execution, they are both the same. This will not affect compilation time, so it has always been suggested to use lambda.

Hope this helps :)

+3
source share

All Articles