Adding a fragment using the add () method does not hide previous fragments

I experience "unexpected behavior" when using the add () method to add a new fragment.

I want to add a new fragment to FrameLayout, but when I do this, the previous fragments will be displayed.

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

  • Is it because I use the FrameLayout and add () methods, just put the fragment on top of the FrameLayout without affecting the previous one?

thanks

+4
source share
2 answers

Not a mistake. Try replacing (..). instead add to the back stack if necessary.

EDIT I think using replace or remove() add() will solve your problem, but since you highlight in your related message, an error that appears in your specific conditions.

+8
source

Another simple thing you can do is 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.

+3
source

All Articles