android - Cannot add multiple fragments to LinearLayout -
i using linearlayout vertical orientation list fragments. add fragments container programmatically this:
fragmenttransaction ft = fragmentmanager.begintransaction(); fragment fragment1 = new fragment(); ft.add(r.id.llcontainer, fragment1); fragment fragment2 = new fragment(); ft.add(r.id.llcontainer, fragment2); ft.commit();
but shows first fragment. why?
you can have multiple fragments in linearlayout
.
according documentation,
if you're adding multiple fragments same container, order in add them determines order appear in view hierarchy
the problem code because didn't specify fragment tags, defaulted container id. since container id same both transactions, 2nd transaction replaced 1st fragment, rather added container separately.
to want, use like:
fragmenttransaction ft = fragmentmanager.begintransaction(); fragment fragment1 = new fragment(); ft.add(r.id.llcontainer, fragment1, "fragment_one"); fragment fragment2 = new fragment(); ft.add(r.id.llcontainer, fragment2, "fragment_two"); ft.commit();
Comments
Post a Comment