Adding a Fragment with add() method doesn't hide previous fragments

流过昼夜 提交于 2020-01-01 09:46:55

问题


I'm experiencing "unexpected behaviour" when using add() method to add a new fragment.

I want to add a new fragment on a FrameLayout, but when I do it the previous fragment stills visible.

  • Is this the expected result when using add() method?

  • It is because I am using a FrameLayout and add() method just place a fragment over the FrameLayout without affect the previous one?

Thx


回答1:


Not a bug. Try replace(..). instead adding to the back stack if required.

EDIT I think that using replace or remove() add() will solve your problem but as you highlight in your related post there is a bug which manifests itself under your particular set of circumstances.




回答2:


Other simple thing what you can do is to call

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

Let me know if this helps.



来源:https://stackoverflow.com/questions/6228401/adding-a-fragment-with-add-method-doesnt-hide-previous-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!