IllegalStateException: Can't change container ID of Fragment

后端 未结 10 1690
孤街浪徒
孤街浪徒 2020-11-29 04:18

Android platform: 3.1

I am trying to move a fragment from a container A to a container B. Here follows the code to accomplish this:

private void rea         


        
10条回答
  •  我在风中等你
    2020-11-29 04:58

    I found the problem was I needed two FragmentTransactions. I was trying to remove() and commit() twice on the same one.

     // create new fragment
     MyFragment myNewFragment = MyFragment.newInstance(this, data, true);
     FragmentManager fm = this.getSupportFragmentManager();
     // clear the back stack
     fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    
     //fragmentTransaction is created for the removal of the old
     FragmentTransaction fragmentTransaction = fm.beginTransaction();
     fragmentTransaction.remove(myOldFragment);
     fragmentTransaction.commit();
     fm.executePendingTransactions();
    
     // fragmentTransaction2 is created to add the new one
     FragmentTransaction fragmentTransaction2 = fm.beginTransaction();
     fragmentTransaction2.add(R.id.fragment_frame, myNewFragment);
     fragmentTransaction2.commit();
     fm.executePendingTransactions();
    

提交回复
热议问题