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
Showing and hiding fragment's viewsUse the FragmentTransaction methods show() and hide() to show and hide the view of fragments that have been added to a container.
You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist. Save this answer.
In ViewPager2 and ViewPager from version androidx. fragment:fragment:1.1. 0 you can just use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume callback is called when fragment became visible and onPause when it stops to be visible.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With