Child fragment's parent returns NavHostParent instead of actual parent fragment

223 Views Asked by At

I have the following structure:

Activity
    NavHostFragment (with navigation graph)
        Fragment1
            ChildFragment
        Fragment2
            ChildFragment
        Fragment3

I want to access either Fragment1 or Fragment2 from ChildFragment class, but when I call parentFragment it returns NavHostFragment instead.

I cannot search directly for Fragment1 or Fragment2 by id or tag because as you can see the parent may change so the ChildFragment doesn't really know the id or name of the parent (unless I send it as argument but I'm trying to avoid that).

1

There are 1 best solutions below

0
On

Ok the problem seems to be that the Fragment1 was adding ChildFragment using parentFragmentManager instead of using childFragmentManager.