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???