I was wondering whether android binding is compatible with live data on conceptual level. There is a simple task: call server after button is clicked. So in my view I have
android:onClick="@{viewmodel::onOrderButtonClick}"
and proper onOrderButtonClick(View) method is defined in ViewModel. But in order to make server call via LiveData I need my Fragment reference (observe() method needs LifecycleOwner instance as first parameter). Of course I cannot hold reference to fragment in my ViewModel. What is the pattern here? Do I really need to implement all event methods in the fragment class and delegate them back into view model class?
After some digging there is a bad news and a good one. The bad news is that the fragment has to be used anyway (there is always some code in the fragment for each livedata event) The good one is that it can be done relatively clean:
In onOrderButtonClick() in view model just call setValue()
That solution in my opinion minimalizes amount of code in the fragment. Still it looks not so elegant to separate making the network call and handling the result