In my code, i have something like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter.onCreate()
fabContainer.onClick {
presenter.onLoginButtonClicked(...)
}
}
When i decompile the apk and check byteCode, something like this appears:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.presenter.onCreate();
Sdk23ListenersKt.onClick((ProgressFloatingActionButton) _$_findCachedViewById(com.prozis.prozisgo.prozisgo.R.id.fabContainer), new ActAuthLogin$onCreate$1(this));
}
final class ActAuthLogin$onCreate$1 extends Lambda implements Function1<View, Unit> {
final /* synthetic */ ActAuthLogin this$0;
ActAuthLogin$onCreate$1(ActAuthLogin actAuthLogin) {
this.this$0 = actAuthLogin;
super(1);
}
public final void invoke(View it) {
this.this$0.getPresenter().onLoginButtonClicked(StringsKt__StringsKt.trim(((EditText) this.this$0._$_findCachedViewById(R.id.email)).getText()).toString(), StringsKt__StringsKt.trim(((EditText) this.this$0._$_findCachedViewById(R.id.password)).getText()).toString(), ((CheckBox) this.this$0._$_findCachedViewById(R.id.rememberEmail)).isChecked());
}
}
Why is this class created and how can i avoid it? Is this normal Kotlin byte code, i tought lambdas were resolved to static methods inside the class they were called in.
A lambda is compiled to a static method if it doesn't reference anything from the surrounding context.
In your example, you're referencing presenter
, which is an instance field of the activity class. In order to capture that reference in the onClick
handler, it's necessary to create an instance of a new class (in this case, Kotlin captures the reference to the activity class and accesses the presenter through its property).
There is no way to avoid creating an extra class here, either with Kotlin or with plain Java.