Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we Use ViewModels to communicate between 2 different fragments or bundles in single activity App Architecture?

Scenario 1 - If we use ViewModels to communicate between fragments, then the ViewModel has to be created by activity reference and hence going to stay there in memory until the activity is destroyed.

Scenario 2 - In master-detail flow ViewModel makes our life easier but again the memory usage issue is there.

Scenario 3 - We have viewModelScope in the new version of arch library to cancel jobs with Fragment/Activity lifecycles, but if ViewModel is created with activity reference then it's going to stay there until activity is destroyed. Hence the job can still be executing and fragment is already gone.

like image 867
Anand Kumar Avatar asked Sep 13 '19 03:09

Anand Kumar


People also ask

Can two fragments use same ViewModel?

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 does by Viewmodels () do?

The ViewModel class allows data to survive configuration changes such as screen rotations. Note: To import ViewModel into your Android project, see the instructions for declaring dependencies in the Lifecycle release notes. The Android framework manages the lifecycle of UI controllers, such as activities and fragments.


1 Answers

You can use ViewModels to communication between two different fragments(aka SharedViewmodels) because it's simple but it's not perfect.

As you know the SharedViewModels must be alive until the first joint parent (activity or fragment) is alive.

but wait ...

What is the purpose of ViewModels?

Are we create ViewModels just for communication between fragments? Absolutely not.

Is the use of ViewModels against the purpose of ViewModels? no, I say it's not perfect use them for communication between fragments but if you have a small project you can use them because it's simple.

So what can I do instead of using ViewModels for communication between fragments? You can build independent UI components and use them for communication between fragments.

What is the advantage of building this weird independent UI components? In this way, each component has its own ViewModel (or Presenter) and they haven't any parent/child relation; Instead, they updated from the same business logic or in reactive programming they are just observing the same Model.

What is the meaning of building independent UI components and how to build them? if you are already familiar with reactive programming, I recommend reading this amazing blog post by Hannes Dorfmann; If you don't, I simply propose using EventBus library for communication between fragments but You will soon realize that too much usage of this library leads to spaghetti code.

like image 105
Shift Delete Avatar answered Sep 28 '22 08:09

Shift Delete