1

I am using navigation component, and I need to pass data back from Fragment B back to Fragment A. I have read from here that I need to use SharedViewModel.

say for example I have a RestaurantListFragment that show list of restaurants. to handle networking and business logic for this fragment I make RestaurantListViewModel .

the second fragment I have is RestaurantDetailFragment. and to handle some actions and business logic in this fragment I create RestaurantDetailViewModel .

now I need to pass data from RestaurantDetailFragment back to RestaurantListFragment, and it is said I need to use SharedViewModel. but now I am confused.

if I use SharedViewModel to pass data, then I will have 2 viewModels in a fragment ? in RestaurantDetailFragment, I will have RestaurantDetailViewModel and XSharedViewModel, and in RestaurantDetailFragment, I will have RestaurantListViewModel and XSharedViewModel ?

so in the the SharedViewModel it only contain the data that need to be passed back to previous fragment ?

or I just need to make make one view model (SharedViewModel) that will serves my two fragments ? (I no longer need to create RestaurantDetailViewModel and RestaurantListViewModel). I am confused.

sarah
  • 3,819
  • 4
  • 38
  • 80
  • 1
    Well, you can have a ViewModel that belongs to the parent activity of your fragments and you can use it to handle common data between your fragments. And each fragment can have its own ViewModel to handle the required logic for each fragment. – Razvan S. Mar 31 '20 at 12:13
  • Don't forget to test for process death if you do this. – EpicPandaForce Apr 17 '20 at 20:41

2 Answers2

0

Both approaches work:

  1. You can use SharedViewModel only for data and methods that are shared between RestaurantDetailFragment and RestaurantListFragment and keep the logic that is only necessary for RestaurantListFragment but not for RestaurantDetailFragment within RestaurantListViewModel and vice versa.

  2. Anyway you can also put all your logic of the RestaurantDetailViewModel and the RestaurantListViewModel inside of the SharedViewModel and get rid of the other 2 ViewModels. While this wouldn't throw any errors, it violates the separation of concerns.

jo3rn
  • 1,291
  • 1
  • 11
  • 28
  • `While this wouldn't throw any errors, it violates the separation of concerns.` what is this nonsense? They are both handling "Restaurant"s. Clearly they have very similar concerns. The reason why you need to separate them is because if a detail can open a detail, then it would cause conflicts. – EpicPandaForce Apr 17 '20 at 20:42
  • Depends on your level of abstraction. It wouldnt be nonsense if you consider restaurant details and a list of restaurants two different concerns. – jo3rn Apr 18 '20 at 10:21
0

You have to create only one View Model only to share or reuse the data between 2 fragments. The concept behind the SharedViewModel is that It will create one object and store the result in LiveData of View Model and will be reused again at different fragment within one Activity Life Cycle attached to it.

You can take a reference form this Github Project if you want

yash786
  • 1,151
  • 8
  • 18