Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap two fragment simultaneously

Tags:

android

In my application i have one activity and i am adding two fragments at run time.I need to swap these two fragment simultaneously. Fragment 1 consist a button and i want when i click that button fragment 1 moves to right side of the screen and other fragment to the left side of the activity.

In the onClick method of the button i tried something like this

    @Override
    public void onClick(View v) {
         FragmentTransaction ft = getFragmentManager().beginTransaction();
         Fragment newFragment = getFragmentManager().findFragmentById(R.id.pageA);
         ft.remove(newFragment);
         Fragment newFragmentB = getFragmentManager().findFragmentById(R.id.pageB);
         ft.remove(newFragmentB);
         ft.add(R.id.pageB, newFragment);
         ft.add(R.id.pageA, newFragmentB);
         ft.addToBackStack(null);
         ft.commit();
     }

But i am getting the following error

 java.lang.IllegalStateException: Can't change container ID of fragment PageA{40653da0   #0 id=0x7f060001}: was 2131099649 now 2131099650

I want something like this when i click the button on Page A then Position of Page A and PageB should swap with each other.

UI of the fragment

like image 643
Deepak Goel Avatar asked Nov 13 '22 10:11

Deepak Goel


1 Answers

I have a similar issue ( IllegalStateException: Can't change container ID of Fragment ) and i solved by swapping the containers instead of the fragments... Nonetheless i still have no clue as to whether it's possibile to swap directly fragments. As I wrote in the aforementioned post, it seems to work only on ics!

like image 174
Matthew Avatar answered Nov 16 '22 03:11

Matthew