Here is what I would like my app to do on the tablet. In fragment (0) there is a menu in which fragments (1) ... (n) will be displayed as follows:
----------------- | | | | | | | | | | |(0)| X | X | X | | | | | | | | | | | ----------------- becomes ----------------- | | | | | | | | | | |(0)|(1)| X | X | | | | | | | | | | | ----------------- and then ----------------- | | | | | | | | | | |(0)|(2)|(1)| X | | | | | | | | | | | ----------------- etc...
Fragment0 never moves, others shift to the right. Fragments emerging from the edge to the right will be destroyed.
So, I am setting up my XML layout with a horizontal LinearLayout and containing 4 FrameLayout with the corresponding identifiers (fragment 0 ... fragment3)
I can create an instance and display fragment 0 and then fragment1, but after that I cannot move it to the right, I get:
ERROR/AndroidRuntime(343): FATAL EXCEPTION: main java.lang.IllegalStateException: Can't change container ID of fragment ...
The only related questions that I found are this one and that one , tried all the different solutions offered with no luck.
I tried FragmentTransaction.remove() , and then .add() , tried .replace() , tried them in different orders and committed halfway (even tried to do twice as much as someone suggested), tried to call addToBackStack() ... still no luck.
The question is whether fragments like this can be moved using FragmentTransaction. If so, what am I doing wrong (and a bonus, can this be revived?). If not, what would be the right way to implement this?
Please note that I do not want to recreate fragments every time (each of them performs some queries on the Internet, which may take some time). It can retrieve all the data back for activity in order to recreate one, but I would rather not do this if it were possible ...