Solution 1 :
The Best Place To add OnClick Listener is View Model when you use MVVM architecture. in MVVM architecture, with data binding, you can handle your on-click listener in many ways.
<Button
onClick="@{()->viewModel.onMyButtonClicked()}"/>
<Button
onClick="@{(view)->viewModel.onMyButtonClicked(view)}"/>
In this, No need to give Id for each. For doing this first you have to register ViewModel into your activity. in Activity onCreate you have to set the content view as I mentioned below.
ActivityMainBinding activityMainBinding = DataBindingUtils.setContentView(this,R.layout.activity_main);
activityMainBinding.viewModel = MyViewModel(application)
activityMainBinding.lifecycleOwner = this;
after this in your layout file, you have to add ViewModel variable
<layout>
<data>
<variable
name="viewModel"
type=".MyViewModel" />
</data>
......
......
. .....
<Button
onClick="@{()->viewModel.onMyButtonClicked()}"/>
<Button
onClick="@{(view)->viewModel.onMyButtonClicked(view)}"/>
</layout>
then If you want to do any changes in Activity, then you have to use Observable variables. that observable variable you have to observe in the activity class. based on the value you have to do the action.
Solution 2 :
According to the documentation ViewModel
is meant to hold Data, and shouldn’t hold a reference to anything a Context
that might have shorter lifecycle. (Activity, Frag, View, Button etc)
Not in the codelab, but in the “Intro to ViewModel” Video they recommend usisng a Presenter class to keep the ViewModel simpler if needed.
Problem :
Coming from MVP to viewModels, I feel I’m a bit lost when it comes to where to place some code.
One example is where to place the click listeners. in MVP I would do something like this
myButton.setOnClickListener { presenter.onMyButtonClicked }
should I be doing the same with a ViewModel? I don’t think so. because it means that I’m treating the viewmodel as if it was a presenter.
But, on the other hand, if I handle the click listener in the view (activity or fragment), the view might not end up as dumb as it should be.
Where is the most suitable place in which a click listener should be handled?
Comments
Comment posted by a_local_nobody
because your question is regarding the implementation of an architecture, i feel like your question might be based on opinion, as there isn’t really any right answer to this, personally i can’t see why you wouldn’t handle this in the view and not in the viewmodel, but again, that’s my
Comment posted by a fair player
@a_local_nobody because the handling might mean that some calculations or logic should be made. and the view is not the right place to do that.
Comment posted by a_local_nobody
exactly, it depends on the situation, there’s no reason why you can’t have the click listener in the view and the implementation logic of the click handled by the VM: function results, callbacks, observable data – all the things available to achieve this, you could use databinding, tons of options, no real
Comment posted by LudvigH
Nice suggestion. This answer would benefit by adding some nuance and discussion on when this approach is good, and when it is less good.
Comment posted by Olkunmustafa
Good answer. It can give an idea even Databinding is not used in your MVVM model.
Comment posted by edit
As it’s currently written, your answer is unclear. Please