Suleyman
Suleyman

Reputation: 2943

Parent fragment to Child Fragment communication

I have been searching and haven't found the best way for nested fragments to communicate with parent fragment. The Android documentation states that:

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Suppose I have a Gallery app, there is a ViewPagerFragment (parent) and ImageFragment(child) that is used to show images full screen when they are clicked. I need to pass an ArrayList of Files from my parent fragment to the child fragment. What is a proper way to do it?

1) Create a getter for the ArrayList in parent fragment and use the getParentFragment() in a child fragment to access this method?

2) Implement an interface and go through the Activity, but how will I locate the child fragment from the Activity if it doesn't have an ID?

3) Or use this in parent fragment:

ChildFragment fragment =(ChildFragment)getChildFragmentManager().findFragmentById(R.id.child_fragment_id);

And just use a setter in child fragment.

But again how do I get the ID of the childFragment?

Thanks everyone in advance

Upvotes: 3

Views: 9631

Answers (3)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

If you want to communicate with your child Fragment to your parent Fragment you can use getParentFragment() method from your child fragment.

Example

public class ExampleParentFragment extends Fragment {

    public int getCount() {
        return 10;
    }
}

public class ChildFragment extends Fragment {


    void doAction() {
        Fragment parentFragment = getParentFragment();

        if(parentFragment instanceof ExampleParentFragment) {
            int count = ((ExampleParentFragment) parentFragment).getCount();
        }
    }
}

Upvotes: 16

Arun Shankar
Arun Shankar

Reputation: 2295

Data should be passed to fragments using Bundle as arguments. So just pass the Arraylist(serialized or otherwise) to your child fragment when launching it

Upvotes: 0

Juan
Juan

Reputation: 5589

I understand that the Fragment to Fragment communication through the activity is for sibling fragments which are coordinated by the same activity.

In this case the siblign fragments are coordinated by a parent fragment, so I would say that the communication should be managed by the parent fragment.

If you are working directly with the fragments then you do that using the ChildFragmentManager().

If you are working with a ViewPager you pass the ChildFragmentManager to the ViewPager insted of passing it the FragmentManager.

If you have to pass a List, I think the easiest is for the ChildFragment to request it when it is ready to receive it by calling a method on the ParentFragment. It can allways get its instace with getParentFragment().

I am not sure where is the safest place to call getParentFrament() to get the list but you can try in onCreateView() ,in onActivityCreated(), o in onResume().

To call the method that returns the list, you can cast the parent Fragment to the specific Fragment class you have extended, or else you can define an interface with the method that returns the List and make your extended parent Fragment implement it.

Then in the child Fragment, you refert to the parent by the interface to call the method and get the list.

Upvotes: 4

Related Questions