Fragments within Fragments
android, android-fragments, android-nested-fragment
Solution
Nested fragments are not currently supported. Trying to put a fragment within the UI of another fragment will result in undefined and likely broken behavior.
Update: Nested fragments are supported as of Android 4.2 (and Android Support Library rev 11) : http://developer.android.com/about/versions/android-4.2.html#NestedFragments
NOTE (as per this docs): "Note: You cannot inflate a layout into a fragment when that layout includes a `<fragment>`. Nested fragments are only supported when added to a fragment dynamically."
Problem
I'm wondering if this is actually a bug in the Android API: I have a setup like so: ``` ┌----┬---------┐ | | | | 1 | 2 | | |┌-------┐| | || || | || 3 || └----┴┴-------┴┘ ``` - Is a menu which loads fragment #2 (A search screen) in the right pane. - Is a search screen which contains fragment #3, which is a result list. - The result list is used in several places (including as a functioning high level fragment in it's own right). This functionality works perfectly well on a phone (Where 1 & 2 and 3 are `ActivityFragment`s). However, when I used this code: ``` FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment frag = new FragmentNumber2(); if(toLoad != null) frag.setArguments(toLoad); transaction.replace(R.id.rightPane, frag); transaction.commit(); ``` Where `R.id.leftPane` and `R.id.rightPane` are `<fragment>`s in a horizontal linear layout. It is my understanding that the above code removes the fragment which is resident and then replaces it with a new fragment. Brilliant... Obviously that isn't what happens because when this code runs the second time you get the following exception: ``` 07-27 15:22:55.940: ERROR/AndroidRuntime(8105): Caused by: java.lang.IllegalArgumentException: Binary XML file line #57: Duplicate id 0x7f080024, tag null, or parent id 0x0 with another fragment for FragmentNumber3 ``` This is caused because the the container for FragmentNumber3 has been duplicated and it no longer has a unique ID. The initial Fragment hasn't been destroyed (?) before the new one is added (in my mind that means it hasn't been replaced). Can someone tell me if this is possible (this answer suggests it isn't) or is it a bug?