Search code examples
androidmvvmviewmodelandroid-viewmodelandroid-mvvm

Is it right to call viewModel's method from view in Android mvvm


I'm building android app using mvvm pattern. I heard that view is recommended not to contain business logic except for UI. I wonder that calling viewModel's method from view is alright.

For example, there is a method that add list A to list B (list A is user's selection).

fun setSelectedList(
    listA: ArrayList<Something>,
    listB: ArrayList<Something>
) {
    listB.addAll(listA)
} //written in kotlin

This method is called from view

Should this method be located in view? or should be located in viewModel?


Solution

  • If you want your data to survive configuration changes like screen rotation, then you should put the method that modifies or stores data in ViewModel and then call that method from your view as and when required. You can refer this for more info.