Home > front end >  Android: One ViewModel for multiple Fragments possible?
Android: One ViewModel for multiple Fragments possible?

Time:12-12

I have a general question about App architecture with Android. I am implementing an App (in Java) that has a single activity and 20Fragments (that are similar but not the same). So far I implemented everything in the fragments (UI, Logic, Database queries). Now I am considering to use a ViewModel but I am not sure whether it is worth the effort. So my question is whether each of my 20 Fragments should have an own ViewModel or whether I can just implement one ViewModel for all the 20 Fragments? Implementing a ViewModel for all Fragment classes would drastically increase the effort, so I would like to know if it is possible to have only one ViewModel for all Fragments?

CodePudding user response:

Have one view model for each fragment. This way each viewmodel is responsible for doing something related to one fragment and so testing is also easier.

In fact you can have multiple view models for a single fragments doing different things for you.

Keeping everything in one view model would make testing harder and also you have to keep viewmodel for all 20 fragments may be scoping to activity.

CodePudding user response:

It is technically possible to have one ViewModel for all Fragments.

However, since this one ViewModel would have to manage a number of very different use cases, it would be something like a god object. With 20 Fragments, it would have very many lines of code ...

Switching over to MVVM is generally worth the effort becasue in the long run the app is easier to test and to maintain.

It may be a good idea to have a BaseViewModel in your app: a ViewModel class which handles things which are similar in all use cases, like letting the Fragment know that it should show a loading indicator or an error message. The "normal" ViewModels could extend BaseViewModel and focus on their use cases.

It makes sense in some cases to have a shared ViewModel, for example when a Fragment shows a dialog with some EditTexts or when one has workflow with a sequence of 3-4 Fragments on a small device where on a larger device one or two Fragments would suffice.

Just in case, here's my favourite starting point for MVVM app architecture: Guide to app architecture

CodePudding user response:

Now I am considering to use a ViewModel but I am not sure whether it is worth the effor

This is definitely worth it if you want good for future development.

So my question is whether each of my 20 Fragments should have an own ViewModel or whether I can just implement one ViewModel for all the 20 Fragments?

Each fragment should have its own ViewModel, I don't appreciate sharing ViewModel. I develop a lot of apps and what I've come to realize is that I should try to avoid tight coupling. Because every time you develop there will be a lot of changes and when you edit or add or remove it will make you edit a lot because the links are too tight. It would be nice to take them apart and still be able to run normally and when needed to be able to reassemble them like putting together puzzle pieces.

Implementing a ViewModel for all Fragment classes would drastically increase the effort, so I would like to know if it is possible to have only one ViewModel for all Fragments?

possible if you want, in programming nothing is impossible. But if you do, you shouldn't because it will only make your code more complicated.

You can read more about the model here.

https://developer.android.com/jetpack/guide#overview

  • Related