Remove old Fragment from fragment manager

后端 未结 5 1882
孤城傲影
孤城傲影 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:17

    I had the same issue. I came up with a simple solution. Use fragment .replace instead of fragment .add. Replacing fragment doing the same thing as adding fragment and then removing it manually.

    getFragmentManager().beginTransaction().replace(fragment).commit();
    

    instead of

    getFragmentManager().beginTransaction().add(fragment).commit();
    

提交回复
热议问题