-1

This is really strange issue, I was scratching my head before posting this.

Straight to the problem: I have a LinearLayout inside a ScrollView in my activity and I use the FragmentManager to pump my Fragments into the layout, each one has its unique Tag and UI, and the code works great.

...
for(object o: my_data)
{
    MyFragment f = MyFragment.newInstance(o.blah());
    Log.w("app","adding object "+o.tag_name()+"to the list");
    transaction.add(R.id.my_linear_layout_id, f, o.tag_name());
}
transaction.commit();

Say I have 3 objects with tag "1","2","3" in my_data. The result of above codes will display my Fragments in right order 1,2,3. In my UI they are 1,2,3 from top of the LinearLayout to bottom. like

1 2 3

Strange issue comes when I try to remove all Fragments and add them with the same code again.(Like refresh)

...
ft = frag_manager.beginTransaction();
for(object o: my_data)
{
    ft.remove(frag_manager.findFragmentbyTag(o.tag_name()));    
}
ft.commit();

The result would become reversed on my LinearLayout, from top to bottom:

3 2 1

Although my Log shows that 1,2,3 has been added to the layout, respectively.

I tried and test the code many times and found that the result is always correct at the FIRST time, but after any remove-all operation, the new result will be reversed and there will be no longer correct result. What could be the reason behind this issue? What am I missing? It's crazy imo.

Ge Rong
  • 416
  • 5
  • 17
  • What kind of Collection is your 'my_data'? – pozuelog Dec 26 '14 at 11:30
  • It's a ``LinkedList``, the above code is straightforward so I just use pesudo code. – Ge Rong Dec 26 '14 at 12:09
  • 1
    I would not expect putting multiple fragments into a `LinearLayout` to be reliable, particularly across configuration changes, when you are no longer the one who is in charge of the order of addition of the fragments. – CommonsWare Dec 26 '14 at 12:13

1 Answers1

0

Operations order on one single FragmentTransaction are not allways respected.

You should try creating news FragmentTransacion in every operation and commit them in you desired order. Related link

Community
  • 1
  • 1
pozuelog
  • 1,284
  • 13
  • 27
  • Well, it works. You save my life, thank you! I've to admit that I did a really serious search on SO to prevent a duplicate for my issue, and failed to find it. And it turned out to be a duplicate lol. – Ge Rong Dec 26 '14 at 12:52