'The fragment is still visible after replacing with another

I have two fragments and one of them is visible by default and I used the code below for it. (all codes are in an activity that is the parent of both fragments)

MainFragment mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,mainFragment).commit();

using a navigation tool user can change the visible fragment and I use this code for changing the fragment.

MarketFragment marketFragment = new MarketFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, marketFragment).commit();

but it seems that this new fragment just appears on top of the previous one and the clicks on main fragment still register.

I also tried removing the main fragment first but it didn't change anything.

getSupportFragmentManager().beginTransaction().remove(visibleFragment).commit();
MarketFragment marketFragment = new MarketFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, marketFragment).commit();

I also tried rebuild and invalidate cache the project but these didn't help either.

what am i doing wrong?



Solution 1:[1]

I think the previous fragment is visible because you have not set a background for them. So, what you have to do is to set a background to the layout files of these fragments like this:

android:background="@color/white"

Or

android:backgroundTint="@color/white"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sambhav. K