问题
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