使用add()方法添加片段不会隐藏先前的片段

Axe*_*cia 5 android fragment android-fragments android-3.0-honeycomb

当使用add()方法添加新片段时,我遇到了"意外行为".

我想在FrameLayout上添加一个新片段,但是当我这样做时,前面的片段仍然可见.

  • 这是使用add()方法时的预期结果吗?

  • 这是因为我使用FrameLayout和add()方法只是在FrameLayout上放置一个片段而不影响前一个?

谢谢

PJL*_*PJL 8

不是错误.尝试替换(..).而是在需要时添加到后台堆栈.

编辑我认为使用replaceremove() add()将解决您的问题,但正如您在相关帖子中突出显示的那样,有一个错误在您的特定情况下表现出来.


Pho*_*ton 6

你可以做的其他简单的事情就是打电话

FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(<your_fragment>);
t.add(<container, <new_fragment>);
..do the rest here..
t.commit();
Run Code Online (Sandbox Code Playgroud)

如果这有帮助,请告诉我。