Proper use of sub sub fragments with (Child)FragmentManager
Asked Answered
R

1

23

How do I properly use Fragments in Fragments?

My (simplified) use case is following, I have an activity with a layout fragment and this fragment theirself contains a sub fragment... all fragments are added manually to their parents...

----------------------------------------------------------
- Activity                                               -
-                                                        -
-                                                        -
-     ---------------------------------------            -
-     - Fragment                            -            -
-     -                                     -            -
-     -    -----------------                -            -
-     -    - SubFragment   -                -            -
-     -    -               -                -            -
-     -    -               -                -            -
-     -    -----------------                -            -
-     ---------------------------------------            -
-                                                        -
----------------------------------------------------------

Now in my activity's onCreate I do following:

if (savedInstanceState == null)
{
    // I create the fragment
    mMainFragment = new MainFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_main, mMainFragment);
    transaction.commit();
}
else
{
    // I retrieve the fragment
    mMainFragment = (BaseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_main);
}

And in my fragments onCreate I get/create my SubFragment:

mSubFragment = getChildFragmentManager().findFragmentByTag(SubFragment.class.getName());
if (mSubFragment == null)
{
    mSubFragment = new SubFragment();
    getChildFragmentManager().beginTransaction().add(R.id.fragment_sub, mSubFragment, SubFragment.class.getName()).commit();
}

Problem

After screen rotation, my SubFragment is added twice... If I use the activity's FragmentManager then it works... But why does it not work with the ChildFragmentManager? Of course, the Fragment is a new fragment, but the activity is a new one as well, so why does it work with the activity's FragmentManager but not with the parent fragment's one?

In a fragment, I should use the fragments ChildFragmentManager, shouldn't I?

Repulsive answered 19/12, 2013 at 8:28 Comment(1)
Not the same question, but for others who come here, see the related Communication between nested fragments in Android.Cargill
Z
12

You should add SubFragment to Fragment the same way like you add Fragment to Activity. I mean adding Fragment to Activity should look like:

 @Override
 public void onCreate(Bundle savedInstanceState) {
   ....
   if (savedInstanceState == null){
      //add fragment
      mMainFragment = new MainFragment();
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.fragment_main, mMainFragment);
      transaction.commit();
   }
 }

Adding SubFragment to MainFragment should look like:

    public class MainFragment extends Fragment{

      @Override
      public View onCreateView(LayoutInflater i, ViewGroup c, Bundle savedInstanceState) {
           ...
        if (savedInstanceState == null){
           mSubFragment = new SubFragment();

           //add child fragment
           getChildFragmentManager()
                   .beginTransaction()
                   .add(R.id.fragment_sub, mSubFragment, "tag")
                   .commit();
        }
      }
    }

or you can add child fragment to Fragment in onCreate method

Zymogen answered 11/3, 2014 at 14:43 Comment(3)
i think this is what the OP posted in question.Heliograph
@Heliograph no, he uses onCreateVIEW vs onCreate. I think this is the correct answerCoston
If fragment A holds B, B holds C then should fragment C also be added using child fragment manager?Scissor

© 2022 - 2024 — McMap. All rights reserved.