2

I stumbled into a crazy little 'bug', or i'm just doing something wrong. I am trying to get the reference to a fragment that is inside a fragment. So we have ParentFragment, that is a child of MainActivity. I can get a reference to this ParentFragment without a problem, because the ParentFragment is added to the MainActivity via code:

ParentFragment fragment = new ParentFragment();

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.fragmentPlaceholder, fragment);
transaction.commit();

I have added a ChildFragment to the ParentFragment via XML like this:

parent_fragment.xml

<RelativeLayout ... etc>
    .. some other views, findViewById('') works great on these.
    <fragment
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/childFragment1"
        android:tag="1"
        class="com.my.package.ChildFragment"
        android:name="com.my.package.ChildFragment"
        tools:layout="@layout/child_fragment" />
</RelativeLayout>

All works fine, the ChildFragment(s) show up in the ParentFragment. The only thing is; i cannot find the fragmentById.

I have tried multiple ways of achieving this, how it should be done (?) :

getChildFragmentManager().findFragmentById(R.id.childFragment1)

Didn't work. Also, getting the Activity and using that fragment manager doesn't work (.. of course).

I have also tried getting all fragments in the ChildFragmentManager, iterating over them etc. But this always returns null:

getChildFragmentManager().getFragments()

Do you have any idea why this is happening? Can't i use the ChildFragrentManager if the fragments are not added via code?

Thanks!

Erik Terwan
  • 2,710
  • 19
  • 28
  • I think you should try to find them using their `Tag` instead. – EpicPandaForce Jan 28 '16 at 11:14
  • Tested it, and doesn't make a difference, note that `getChildFragmentManager().getFragments()` also returns null. – Erik Terwan Jan 28 '16 at 11:18
  • oh. I haven't really used child fragments before, but isn't there a method something like `getSupportChildFragmentManager()`? Considering these seem to be support fragments, if that exists, that's the one that has it. – EpicPandaForce Jan 28 '16 at 11:20
  • No only getChildFragmentManager.. Let me try to make everything regular fragments instead of support fragments – Erik Terwan Jan 28 '16 at 11:30
  • 1
    Method getChildFragmentManager of support fragment returns support fragment manager. Support fragments naturally do not support native child fragments. – Eugen Pechanec Jan 28 '16 at 11:41
  • Yes, that's it. I had to strip any reference of FragmentActivity and the supportFragmentManagers etc. Now it works like a charm. – Erik Terwan Jan 28 '16 at 11:53

2 Answers2

5

You must add your child fragment dynamically. See here:

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.

ps. if you are planning to use nested fragments then be prepared for various strange behaviours, you can find them in bug reports to android platform:

https://code.google.com/p/android/issues/list?can=2&q=nested+fragment&sort=-opened&colspec=ID+Status+Priority+Owner+Summary+Stars+Reporter+Opened&cells=tiles

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • This isn't a must, as long as you don't use the support classes. – Erik Terwan Jan 28 '16 at 11:53
  • @ErikTerwan there are also issues with non-support nested fragments (or maybe they fixed them lately), you can read on it here: https://code.google.com/p/android/issues/detail?id=58906&q=nested%20fragment&sort=-opened&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened – marcinj Jan 28 '16 at 12:10
  • Yeah the thing about ChildFragments still existing is not something i am noticing, after removing the ParentFragment and adding it again, all is good. The fragments are recreated like they should. So i guess they fixed that issue..? – Erik Terwan Jan 28 '16 at 12:32
  • Probably, the problem with using native fragment support is that if you run it on older devices then those fixes will (probably) be not applied, so you would have to always test your code on all supported API versions. With support library its easy - support library is always inside your APK – marcinj Jan 28 '16 at 12:34
0

If you want to use nested Fragments and be able to get a reference to them via findFragmentById you cannot use the support package.

I changed all getSupportFragmentManager()'s to getFragmentManager() and replaced all FragmentActivity's with regular Activity's.

Now it works as expected, just call getChildFragmentManager().findFragmentById().

Erik Terwan
  • 2,710
  • 19
  • 28