Android ChildFragmentManager().findFragmentById() always null -
i stumbled crazy little 'bug', or i'm doing wrong. trying reference fragment inside fragment. have parentfragment
, child of mainactivity
. can reference parentfragment without problem, because parentfragment
added mainactivity
via code:
parentfragment fragment = new parentfragment(); fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.fragmentplaceholder, fragment); transaction.commit();
i have added childfragment
parentfragment
via xml this:
parent_fragment.xml
<relativelayout ... etc> .. 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, childfragment(s) show in parentfragment. thing is; cannot find fragmentbyid.
i have tried multiple ways of achieving this, how should done (?) :
getchildfragmentmanager().findfragmentbyid(r.id.childfragment1)
didn't work. also, getting activity , using fragment manager doesn't work (.. of course).
i have tried getting fragments in childfragmentmanager, iterating on them etc. returns null:
getchildfragmentmanager().getfragments()
do have idea why happening? can't use childfragrentmanager if fragments not added via code?
thanks!
you must add child fragment dynamically. see here:
note: cannot inflate layout fragment when layout includes <fragment>. nested fragments supported when added fragment dynamically.
ps. if planning use nested fragments prepared various strange behaviours, can find them in bug reports android platform:
Comments
Post a Comment