The general idea is that the XML Layout have a reference to the ViewModel like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout">
<data>
<variable
name="aboutViewModel"
type="com.app.about.AboutActivity" />
</data>
The question is that if is a bad practique expose too the activity like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout">
<data>
<variable
name="aboutActivity"
type="com.app.about.AboutActivity" />
</data>
Because sometimes I only need call functions for open or close the activity, and I think that these are not a reason enough to create a new ViewModel.
Thanks
If you follow the OOB - separation of concerns then you shouldn't tightly couple the VM
with Activity
. Also you don't have to create an new VM
or to tightly couple your current VM
, you can just access the activity/context like this:
android:onClick="@{(view)->((Activity)(view.getContext()))}"