Android Fragment - move from one View to another?

后端 未结 6 517
有刺的猬
有刺的猬 2020-12-16 18:04

Can i first add a Fragment to a View, then \"detach\" it, and then \"re-attach\" it to another View?

In code, i want to:

fragOne one = new fragOne();         


        
6条回答
  •  死守一世寂寞
    2020-12-16 18:30

    I guess you would have this figured out by now, but i dont't see any satisfactory answer. So, I'm posting this for others who may refer to this in the future.

    If you want to move a fragment from one view to another you do the following:

    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.remove(fragment1);
    fragmentTransaction.commit();
    fragmentManager.executePendingTransactions();
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.containerToMoveTo, fragment1);
    fragmentTransaction.commit();
    

    This way you do not have to duplicate the fragment.

提交回复
热议问题