1

Googling I see many declarations that nested fragments cannot use XML. Now I am new to Android, but my app is using XML with nested fragments. I haven't got the listeners and interfaces working yet (maybe this is why people say you can't use XML), but the GUI works.

My question: What is meant by the comments I have read about not using XML for Nested Fragments?

Here a link to one stating XML can't be used with nested fragments:

The code below creates 3 radiogroups (each in a fragment) horizontally arranged on top (in another fragment) with a listview (in another fragment) below them. The fragments allow good control on the look for different display types.

Here is my code:

public class SetupNew extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ngs);   
}}

ngs.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<fragment android:id="@+id/frag_options_all"
          android:layout_height="250dp"              
          android:layout_width="fill_parent"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:name="com.EXAMPLE.frag_class_options_all"/>
<fragment android:id="@+id/frag_select_opponent"
          android:layout_height="fill_parent"
          android:layout_marginLeft="5dp"
          android:layout_marginTop="10dp"
          android:layout_width="fill_parent"
          android:name="com.EXAMPLE.frag_class_opponents"/>
</LinearLayout>

frag_class_options_all.java

    public class frag_class_options_all extends Fragment {
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.options_all, container, false);
      return view;
  }}

frag_class_opponents.java

public class frag_class_opponents extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {  
    //working contact listview
}

options_all.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<fragment android:id="@+id/frag_options"
          android:layout_height="fill_parent"              
          android:layout_width="80dp"
          android:layout_marginRight="15dp"
          android:name="com.EXAMPLE.frag_class_options"/>
<fragment android:id="@+id/frag_ship_limit"
          android:layout_height="fill_parent"              
          android:layout_width="75dp"
          android:layout_marginRight="15dp"
          android:name="com.EXAMPLE.frag_class_limit_options"/>
<fragment android:id="@+id/frag_allowable_ship"
          android:layout_height="fill_parent"              
          android:layout_width="fill_parent"
          android:name="com.EXAMPLE.frag_class_allow"/>
</LinearLayout>

frag_class_options, frag_class_limit_options and frag_class_allow all follow something like this:

public class frag_class_options extends Fragment{
RadioGroup radioGroup;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.options_m, container, false);

    radioGroup = (RadioGroup) view.findViewById(R.id.rg_limit);     
    return view;
}     
}
Community
  • 1
  • 1
user2383867
  • 117
  • 10

3 Answers3

3

In order to nest fragments, you must use the getChildFragmentManager method of a Fragment. The default XML implementation of fragments in XML uses the getFragmentManager of an Activity which leads to the error. Since the childFragmentManager can only be accessed through Java code, this is why you can't use XML for nesting fragments

See more information: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Stevie Kideckel
  • 1,928
  • 2
  • 17
  • 21
  • 3
    TL:DR Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically. – Robin Vinzenz Jan 19 '16 at 08:34
2

This Duplicate id bug is why you shouldn't declare your nested fragments in xml.

Declaring nested fragments in xml is not forbidden in android and it works as long as you do not destroy the fragment and try to recreate it (which happens inside of the fragmentManager.replace) So for example if you have a navigation drawer and do the following:

fragManager.replace(containerId, new fragmentOne()); fragManager.replace(containerId, new fragmentTwo());

fragManager.replace(containerId, new fragmentOne());

The 3rd replace will probably crash with the duplicate id bug. I don't know why this happens but I can assure you it does and it is very frustrating! Personally I wish nested fragments failed completely in xml rather than partially working!

Community
  • 1
  • 1
Alec Holmes
  • 3,625
  • 4
  • 22
  • 23
  • Thanks for this! I was skeptical as to this "no nested fragments in XML" prohibition, especially since it seemed to work just fine. But sure enough, after testing with a few orientation changes, I saw that "duplicate ID" crash. Saved me from loads of bug-hunting down the line. – MandisaW Mar 15 '18 at 21:02
-1

My question: What is meant by the comments I have read about not using XML for Nested Fragments?

Answer: I have no idea. You haven't told us where you found these comments, so we have nothing to go on. Perhaps the author of the comments defined what he meant by the term "nested fragment", perhaps he didn't. It's not a standard piece of XML terminology, and I can't guess what it means from your question. I see elements called "fragment" in your XML sample, but they aren't nested.

Perhaps the question is very specific to android, in which case you shouldn't have tagged it as a general XML question.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164