I have an xml layout for a Fragment (android.support.v4.app.Fragment). When this Fragment is added for the first time, the background drawable displays fine. But when this Fragment is replaced with another Fragment, then later replaced back in again (by creating a new instance and using FragmentTransaction.replace()), the background drawable disappears (but not in all cases, see below). Here is the layout xml for the Fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/footer_photos" />
<RelativeLayout
android:id="@+id/pageLayout"
android:background="@drawable/body_background2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer">
<ImageView
android:id="@+id/page"
android:layout_height="500dp"
android:layout_width="400dp"
android:layout_centerInParent="true"
android:background="#FFF" />
</RelativeLayout>
</RelativeLayout>
The background drawable in question is in the RelativeLayout with the id “pageLayout”.
Possible clues:
- When I remove the ImageView in there, the background drawable shows up fine.
- The other Fragments have similar layout xmls (with the same background drawable), but without ImageViews inside of them, and they work fine.
- One of these Fragments has a WebView and buttons inside of it, and this one’s background shows up fine.
- After replacing the “WebView Fragment” with the Fragment in question, the background shows up fine (!?).
EDIT: The same issue consistently occurs if an image is loaded into the ImageView then a dialog-themed activity is launched on top of it, then finished. My solution below fixes both cases.
Found a better solution than my last one. In onResume() for the Fragment, just need to do:
It causes a flicker, but at least it seems to work consistently.