Reputation: 389
I have searched for solution and found so many cases (not relevant to my situation or may be i didn't understood them properly).
My case is :
Let's say i have one parent Fragment Parent;
And i'm creating two child fragments form parent fragment's onCreateView
ChildFragment1 child1 = new ChildFragment1();
ChildFragment2 child2 = new ChildFragment2();
//My child fragment exactly looks like this
public class ChildFragment1 extends Fragment {
View rootView;
public ChildFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.child_layout, container, false);
return rootView;
}
public void methodTobeCalled() {
//here i would like to do some changes to child view (add some views)
}
}
i can access method of child fragment through reference of child like
child1.methodTobeCalled();
But at this time i don't have access to my child Fragment View.
I would like to call my child Fragment function from parent fragment when i have access to child view, is there any chance?
Thanks in advance :)
Upvotes: 3
Views: 6878
Reputation: 1209
You can archive by using getChildFragmentManager().
Here is the code
YourFragment yourFragmentinstance = (YourFragment) getChildFragmentManager().getFragments().get(**FRAGMENT_POSITION**);
yourFragmentinstance.yourMethod();
FRAGMENT_POSITION is maybe 0,1 depends on your initialization.
Upvotes: 1
Reputation: 716
for access to child_fragment
views from parent_fragment
, use this code:
rootView = inflater.inflate(R.layout.my_fragment, container, false);
myET = rootView.findViewById(R.id.et_my);
Upvotes: 0
Reputation: 1009
try to attach it inside this method
@Override
public void onAttachFragment(Fragment childFragment) {
super.onAttachFragment(childFragment);
}
Upvotes: 1