Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be done in Activity/Fragment and ViewModel in MVVM

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
    }
}
like image 302
Tar_Tw45 Avatar asked Nov 08 '16 11:11

Tar_Tw45


People also ask

What is the use of ViewModel in MVVM?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

Can a ViewModel be shared between activity and fragment?

In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel. This is one way to have communication between fragments or activities.

What is the purpose of using fragments in an activity?

Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.

How to create a ViewModel from a fragment?

Use activity's scope to create an instance of the ViewModel in fragment so that all the fragments and activity now has a common ViewModel and have access to the same ViewModelStore. If the activity is re-created, it and all its fragments receive the same ViewModel instance that was created by the associated activity.

What is viewmodel (MVVM)?

Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns. MVVM suggests separating the data presentation logic (Views or UI) from the core business logic part of the application. The separate code layers of MVVM are:

What is MVVM design pattern?

Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns. MVVM suggests separating the data presentation logic (Views or UI) from the core business logic part of the application.

What are the separate code layers of MVVM?

The separate code layers of MVVM are: 1 Model: This layer is responsible for the abstraction of the data sources. Model and ViewModel work together to get and save the data. 2 View: The purpose of this layer is to inform the ViewModel about the user’s action. ... 3 ViewModel: It exposes those data streams which are relevant to the View. ...


1 Answers

Yes you can have all business logic in your ViewModel, Here are some links which i personaly follows to learn MVVM

Approaching Android with MVVM
https://github.com/ivacf/archi
MVVM on Android: What You Need to Know

You can mention all your listeners in ViewModel as well as data which your model will consist.

ViewModel alters some content and notifies the binding framework that content has changed.

Model - Data model containing business and validation logic
View - Defines the structure, layout and appearance of a view on screen
ViewModel - Acts a link between the View and Model, dealing with any view logic

enter image description here

reference

like image 112
Ravi Avatar answered Oct 10 '22 09:10

Ravi