In the recently released book, Best Recommendations for Android, I read that MVVM is a good design pattern for android programming. Having tried it myself in my last project, it seems that it is beneficial to split the code into more manageable sections.
The view only handles the creation of view and interface elements with the ViewModel. ViewModel implements the interface and processes operations on the presentation 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 on the Internet and cannot find much information that supports it as a good design template for Android. I also have a few questions, such as:
ViewModel ?
?
, fragmentManager?
, ?
- - - , MVVM???