Android data binding - Can I access closing activity in an XML layout?

I would like to define an event listener in XML that completes the closing activity, for example:

<EditText
    android:id="@+id/finish"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:onEditorAction="@{() -> activity.finish()}" />

However, I have no reference to the activity involved. I know that I can pass it using <variable>, but Activityit seems too common a variable that should be explicitly passed in each layout ... I thought that data binding was introduced to simplify the code. I did not find any hint on the ViewDataBinding class .

+4
source share
1 answer

, . , , . android.com .

, , , - Activity:

<EditText
    android:id="@+id/finish"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:onEditorAction="@{() -> ((Activity)context).finish()}" />
+2

Source: https://habr.com/ru/post/1648928/


All Articles