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

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -