Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a view of parent fragment from child fragment in navigation component

I need to access a view of parent fragment from child fragment. enter image description here

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.

like image 288
ali-star Avatar asked Dec 20 '18 09:12

ali-star


2 Answers

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.

like image 102
Anmol Avatar answered Sep 19 '22 08:09

Anmol


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)
like image 23
PSK Avatar answered Sep 20 '22 08:09

PSK