Android: Replaced Fragment Is Still Visible

On Android, if you replace a fragment using FragmentTransaction.replace, the old fragment could still be seen. This is most often caused by not setting the background color of the fragment.

Below is a screenshot of the main fragment.

fragment-main

When the fragment is replaced, both fragments become visible.

fragment-2

To solve the problem, set the android:background property to “?android:windowBackground” in the fragment layout files.

The “?android:windowBackground” property value is based on the current theme, so no hard coding of the background color is involved. After making the changes, the replaced fragment will appear as shown below.

fragment-2-ok

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }