Remove old Fragment from fragment manager

后端 未结 5 1873
孤城傲影
孤城傲影 2020-11-27 15:26

I\'m trying to learn how to use Fragments in android. I\'m trying to remove old fragment when new fragment is calling in android.

5条回答
  •  遥遥无期
    2020-11-27 16:09

    If you want to replace a fragment with another, you should have added them dynamically, first of all. Fragments that are hard coded in XML, cannot be replaced.

    // Create new fragment and transaction
    Fragment newFragment = new ExampleFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);
    
    // Commit the transaction
    transaction.commit();
    

    Refer this post: Replacing a fragment with another fragment inside activity group

    Refer1: Replace a fragment programmatically

提交回复
热议问题