5

An Android Viewmodel can either have the scope/lifespan of either an activity or fragment, but how will this work in a Navigation component?

Considering if I have the following scenario:

MainActivity 
 - Contains the NavHostFragment

CustomerListFragment 
 - Starting point for the navigation
 - On click of any customer in the list, will navigate the CustomerDetailsFragment

CustomerDetailsFragment
 - Contains a tab with 3 fragments: CustomerDetailFragment, CustomerContactFragment and CustomerOrderHistoryFragment

CustomerDetailFragment
 - Display customer general information such as name, age, gender, etc...
 - Get data from CustomerViewModel

CustomerContactFragment
 - Display customer address, tel, fax, etc
 - Get data from CustomerViewModel

CustomerOrderHistoryFragment
 - Display customer's order history
 - Get data from OrderHistoryViewModel

On the scenario above, is it possible to share the same instance of CustomerViewModel for CustomerDetailFragment and CustomerContactFragment?

If I wasn't using the navigation component, I could just make CustomerDetailsFragment an activity and init my viewmodel to that activity but since in Navigation we only have fragments, how can I best achieve this?

I though of:


public class CustomerDetailsFragment extends Fragment {
...
CustomerViewModel model = ViewModelProviders.of(this).get(CustomerViewModel .class);
...

but how do I pass this instance of viewmodel to CustomerDetailFragment and CustomerContactFragment?

Or is there a better way to structure the code above to achieve the same result?

3
  • If you want to use same instance of viewmodel in CustomerDetailFragment and in CustomerContactFragment you need to attach it to your activity, i think it is the only option. CustomerViewModel model = ViewModelProviders.of(getActivity()).get(CustomerViewModel .class); And it is a good solution.
    – Alex
    Commented Jun 20, 2018 at 4:24
  • In that case, if I have a much more bigger navigation_graph. Won't the CustomerViewModel lifespan be thoughout the whole navigation? Commented Jun 20, 2018 at 4:44
  • It will, it is all depends on what information you want share between fragments, You can have one MainViewModel that attached to your activity, this viewmodel will contain list of customers, and when user clicking on any customer you will pass the id of the customer to CustomerDetailsFragment, and from there to CustomerDetailFragment and CustomerContactFragment and each of them can retrieve the proper customer from MainViewModel customers list(it will be the same customer instance).
    – Alex
    Commented Jun 20, 2018 at 5:10

2 Answers 2

5

You can use parentFragment as a store owner. Call this in your Detail and Contact fragments.

CustomerViewModel model = ViewModelProviders.of(getParentFragment()).get(CustomerViewModel.class);
3
  • And here I am again facing the same problem and you helped me again! Commented Nov 9, 2018 at 15:53
  • Excellent. This answers my comment above. Commented Feb 11, 2019 at 8:28
  • This strategy is falling apart now. Scoping to the nav host fragment is too inflexible. I want multiple instances of a viewmodel within a flow that are shared between some fragments only, but it's not possible Commented Feb 28, 2019 at 6:19
1

your scenario is well supported by ViewModel.

You just need to get ViewModel with activity scope: ViewModelProviders.of(getActivity).get(...

More info here: https://developer.android.com/topic/libraries/architecture/viewmodel#sharing

5
  • How should the lifecycle of the viewmodel be handled with this strategy? When we navigate away from the customer section of the app, the CustomerViewModel will stay in memory until the user exits the app, unless we specifically clear it somehow Commented Feb 11, 2019 at 8:25
  • VM should stay in Activity scope (will be tied to its lifecycle)
    – kzotin
    Commented Feb 11, 2019 at 17:01
  • Since there is only one activity if using Android Navigation, that means the whole app scope. I don't think thats a good idea as memory can never be released for a shared viewmodel Commented Feb 11, 2019 at 23:25
  • In this case, you should have parent fragment & child fragments - you should tie VM to parent fragment scope (instead of whole Activity)
    – kzotin
    Commented Feb 12, 2019 at 9:17
  • Yeah that's what I went with Commented Feb 12, 2019 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.