I need to access a view of parent fragment from child fragment.
I have a graph inside a fragment (my parent fragment) and i want to access a view that present in the parent fragment from child fragment, i'm using android navigation component, it's confused me a little bit.
As i read your Explanation's You Know that ChildFragment Parent is NavHostFragment and NavHostFragment's Parent is ParentFragment.
NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
Fragment parent = (Fragment) navHostFragment.getParentFragment();
parent.getView().findViewById(R.id.element_id);
I have tried is my demo Project and work's for me.
Using Activity:
val buttonView = (context as Activity).findViewById<View>(R.id.map_list_card_view)
Using Fragment:
val parent: Fragment? = (parentFragment as NavHostFragment).parentFragment
val buttonView = parent?.view?.findViewById<View>(R.id.map_list_card_view)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With