The problem is quite straightforward. The question is in context of using ViewModels, LiveData and other related Lifecycle aware arch approaches.
I have an Activity with Na
What i would suggest you can do is handle two ViewModel
for your entire use case.
Make one ViewModel
Let's say MyActivityViewModel
to handle all logic related for activity level. So, if any fragment logic is directly related to your activity then share your ViewModel
like below :
ViewModelProviders.of(getActivity()).get(MyActivityViewModel.class); // Like this in fragment.
&
ViewModelProviders.of(this).get(MyActivityViewModel.class); // Like this in activity.
This will share common ViewModel
between your activity and fragment.
Another ViewModel
would go for FirstFragment
in your case if you have to share logic between your ChildFragment
:
Here you can share ViewModel
let's say FragmentViewModel
like below:
ViewModelProviders.of(this).get(FragmentViewModel.class); // Like this in FirstFragment which is having view pager.
&
ViewModelProviders.of(getParentFragment()).get(FragmentViewModel.class); // Like this in View pager fragments, getParentFragment() is First fragment in our case.
Although, we can still use our activity level MyActivityViewModel
in our child fragments from FirstFragment like :
ViewModelProviders.of(getActivity()).get(MyActivityViewModel.class);