2

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 :)

Suresh Gogu
  • 359
  • 5
  • 12

3 Answers3

1

try to attach it inside this method

@Override
public void onAttachFragment(Fragment childFragment) {
    super.onAttachFragment(childFragment);
}
Ankit Aman
  • 999
  • 6
  • 15
1

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.

Arul
  • 1,031
  • 13
  • 23
0

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);
wscourge
  • 10,657
  • 14
  • 59
  • 80
roghayeh hosseini
  • 676
  • 1
  • 8
  • 21