Android MVVM Design Pattern

android, android-activity, android-fragments, design-patterns, mvvm

Solution

I will try to give my opinion. I think the sample code you gave didn't follow the core value of applying MVVM(or presentation model. MVVM is originated from presentation model) pattern. One of the major motive of the pattern is to make ViewModel(or Presentaion Model) pure POJO so that ViewModels allow maxmium testability. I have not read the book, but i recommend you to read Martin Fowler's original article about the pattern. I created some examples to demonstrate how to apply the pattern in Android development. If you are interested, you can have a look here - Album Sample, which is an android translation of Martin Fowler's original album example, and AndroidMVVM, a minimal demo app.

One way to apply the pattern is: View(Activity or fragment+layout), ViewModel, Model(business model: persistence layer, networking etc..). With this approach, to answer your question, i think one fragment maps to one ViewModel.

The pattern is to improve the design. When applied correctly, it will reduce the complexity not the other way around. Hope this helps.

Problem

I have read in the recently released 'Android Best Practices' book that a good design pattern to use for android programming is MVVM. Having tried it myself on my latest project it does seem to be beneficial in separating code into more manageable sections. The View only handles creation of view items and an interface to a ViewModel. The ViewModel implements the interface and handlss operations on the view and interaction with the Model. Sample code below: Model ``` public class MyModel{ public String myString; public MyModel(String myString){ this.myString = myString; } } ``` View ``` public class MyActivity{ public ViewManager delegate; public interface ViewManager{ void registerTextView(TextView tvText); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); delegate = new ViewController(this); TextView tvText = (TextView) view.findViewById(R.id.tvText); delegate.registerTextView(tvText); } } ``` ViewModel ``` public class ViewController implements MyActivity.ViewManager{ Context activity; TextView tvText; MyModel myModel; public ViewController(Context app_context){ activity = app_context; myModel = new MyModel("Hello World"); } @Override public registerTextView(TextView tvText){ this.tvText = tvText; tvText.setText(myModel.myString); } } ``` However, I have not seen this approach anywhere else online and am unable to find much information that supports it being a good design pattern for android. I also have a few questions such as : Should you have a separate ViewModel for every fragment or just Activities? Does this approach perform well on configuration change and Activity recreation with the extra overhead of another class? Can you cast the context to your activity to enable use of the fragmentManager? How does this scale as code gets more complex? Does anyone have experience using this design pattern with android or could anyone point me in the direction of some good study material before i start converting all my projects to MVVM???

Original source