So I'm using the new navigation component (with the one activity principle) and communicating between each fragment using shared view models, however, I've come to a point where I sometimes need to clear the view model but I can't find a good place to clear it. But tbh I think rather than trying to clear it myself, I should really be allowing the framework to do it for me, but it doesn't because the view models are shared and scoped to the activity, but I think I can scope them to a parent fragment, I made a drawing to illustrate what I'm trying to do. so I only want to clear the 2 view models when I navigate back from "Child 1 Child a" currently the view models are never cleared, trying to get the view model currently by calling 'this' in the fragment and getParentFragment in the child doesn't work, can anyone provide an example?
EDIT
It looks like I was already doing something similar but it's not working in my case so I will add some code, here is how I access the first view model in the parent fragment
model = ViewModelProviders.of(this).get(RequestViewModel.class);
and then in the child fragment, I'm doing this
requestViewModel = ViewModelProviders.of(getParentFragment()).get(RequestViewModel.class);
but it's not keeping the data between them, they both have observers attached
ViewModels
in parent & child fragment just to verify that they're shared? If they're both same meaning they're shared.. also check for your child of child fragments. It may be cause. – Thimerosal