0

I am creating a Fragment FragParent displaying several input-fields, one of which is a sub-Fragment frag, with specialized audio controls.

The layouts of both fragments are loaded from XML in their onCreateView() methods. Code I am using is below, but I have some questions about it:

A) is it OK to replace() the sub-Fragment if it is already returned by findFragmentByTag(), or is that an unnecessary step?

B) can the sub-Fragment be instantiated and replaced in the layout, before the parent Fragment completes the inflate() call?

<!-- language: lang-java  -->
// Load sub-Fragment with audio UI.
String fragStr = "fragment_audio_str";
int fragView = R.id.frag_audio_frame_layout;
FragmentManager fm = getChildFragmentManager();
FragmentTransaction fmt = fm.beginTransaction();
Fragment frag = (Fragment)  fm.findFragmentByTag(fragStr);
if (null == frag) {
    frag = new Fragment();
}
fmt.replace(fragView, frag, fragStr); 
fmt.commit();

return inflater.inflate(R.layout.fragment_parent, container, false);
} // END onCreateView() of FragParent
donfede
  • 734
  • 1
  • 9
  • 25

2 Answers2

3

A) is it OK to replace() the sub-Fragment if it is already returned by findFragmentByTag(), or is that an unnecessary step?

No, it shouldn't be necessary to replace the fragment if it is already there. However you need to replace your child fragment if it isn't there.

B) can the sub-Fragment be instantiated and replaced in the layout, before the parent Fragment completes the inflate() call?

No, you need to inflate the parent Fragment layout and then add your child Fragment to it before returning the parent's View.

Try something like this:

        View parentFragLayout = inflater.inflate(R.layout.fragment_parent, container, false);
        FrameLayout childFragContainer = (FrameLayout) parentFragLayout.findViewById(R.id.frag_audio_frame_layout);

        // Load sub-Fragment with audio UI.
        String fragStr = "fragment_audio_str";
        FragmentManager fm = getChildFragmentManager();
        Fragment frag = fm.findFragmentByTag(fragStr);

        // Child fragment isn't there, so add it
        if (frag == null) {
            frag = new Fragment();
            FragmentTransaction fmt = fm.beginTransaction();
            fmt.replace(childFragContainer.getId(), frag, fragStr);
            fmt.commit();
        }
        return parentFragLayout;
Steven Byle
  • 13,149
  • 4
  • 45
  • 57
0

Unless your Fragment is contained your XML layout (which, since you're adding it in the onCreateView, I'm assuming that it is not contained in your XML layout) you will still need to use FragmentManager to add the Fragment. However, you should use add() instead of replace(). You should only call replace() when you are actually replacing another Fragment.

While I'm not a hundred percent sure about the second question, I'm pretty sure that it's a no.

Don
  • 506
  • 2
  • 8
  • 23
  • "you should use add() instead of replace()" -- what's your basis for that? What if it's getting called again after being created? – donfede Feb 04 '13 at 01:38
  • My only basis for that is the documentation. From personal experience, I've found that it is usually best to use methods for their intended purpose. To implement this I would override the appropriate Fragment life cycle callbacks and use a Bundle object to tell if the Fragment is being recreated with a previous saved state. – Don Feb 04 '13 at 03:08