Our company has been developing Android application using MVP pattern a while. With MVP, we put all business logic inside the presenter and the Activity/Fragment then just responsible for view update when receiving event callback from presenter.
Now, we decided to try MVVM using Android Databinding. It seems that with MVVM, we can put all the business logic in the ViewModel (just like Presenter in MVP) and also notify the view(s) of any changes to data model, all in one object.
But then, this raise question in our mind, what should we left to be handle by the Activity/Fragment? Since we adopted the MVP pattern to avoid fat-activity/fragment. We don't want to have slim-activity/fragment and then fat-viewmodel.
What we think we can left to be handle by Activity/Fragment so far
- Request/Check permission
- Access Context
- Access Resources
Every correction, comment or suggestion are welcome since I'm fairly new to MVVM, even if it seems to be similar to MVP.
Thank you.
A bit more question
Is it possible and good practice to combine MVVM with listener (like MVP)? For example
public class MainActivityViewModel extends BaseObservable {
MainActivityViewModelListener listener;
User user;
public void setMainActivityViewModelListener(MainActivityViewModelListener listener) {
this.listener = listener;
}
public void refreshUser(View v) {
// some user update via Internet
notifyPropertyChanged(BR.userAlias);
if (listener != null) {
listener.onUserRefreshed(user);
}
}
@Bindable
public void getUserAlias() {
return user.getAlias();
}
}
public interface MainActivityViewModelListener {
void onUserRefreshed(User user);
}
public class MainActivity implements MainActivityViewModelListener {
MainActivityBinding binding;
@Override
public void onCreate(Bundle savedInstanceState) {
binding = DataBindingUtil.setContentView(R.layout.main_activity);
MainActivityViewModel viewModel = new MainActivityViewModel();
viewModel.setMainActivityViewModelListener(this);
binding.setMainActivityViewModel(viewModel);
}
@Override
public void onUserRefreshed(User user) {
// do some update
}
}