Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] mvvm – Android ViewModel and click listeners

Posted on November 14, 2022

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)

A good codelab here

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.

READ  [ANSWERED] Android Volley: Sending many request caused app to close
Powered by Inline Related Posts

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

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress