Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share ViewModel between Parent fragment and fragment inside child fragment

I have parent Fragment that contains child fragment. Inside child fragment I have ViewPager with fragments. My question is how can I share ViewModel between parent child and fragments in viewpager and makeing Viewmodel visible only on ParentFragment scope?

like image 792
Rasul Avatar asked Jan 20 '26 16:01

Rasul


1 Answers

what do you mean when you say "visible only on ParentFragment scope"?

According Google's document, there is one way that you can share ViewModel. Check this document: https://developer.android.com/topic/libraries/architecture/viewmodel#sharing Shortly, your parent fragment and child fragment will use the same ViewModel. Your parent fragment will call the function of ViewModel to change the data, your child fragment just observer the LiveData of ViewModel.

like image 78
NhatVM Avatar answered Jan 23 '26 05:01

NhatVM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!