Android device configuration change and setRetainInstance, crash the second time
<p>I am writing an activity that contains a single gragment.
This gragment displays a set of photos with a "AdapterViewFlipper" and a "SimpleCursorAdapter".</p>

<p>To have a quick response on device's orientation change, I use the fragment "setRetainInstance" feature.</p>

<p>It works quickly and nicely on the first orientation change, without reloading images from the network.</p>

<p>The problem appears on the second orientation change, with a "IllegalArgumentException" in the fragment "onDetach" :</p>

<pre><code>09-30 13:50:36.374: W/System.err(8310): java.lang.IllegalArgumentException: Receiver not   registered: android.widget.AdapterViewFlipper$1@424512f8
09-30 13:50:36.384: W/System.err(8310):     at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
09-30 13:50:36.384: W/System.err(8310):     at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1134)
09-30 13:50:36.384: W/System.err(8310):     at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:378)
09-30 13:50:36.384: W/System.err(8310):     at android.widget.AdapterViewFlipper.onDetachedFromWindow(AdapterViewFlipper.java:112)
09-30 13:50:36.384: W/System.err(8310):     at android.view.View.dispatchDetachedFromWindow(View.java:11789)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2534)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3819)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.removeAllViews(ViewGroup.java:3765)
09-30 13:50:36.384: W/System.err(8310):     at debut.telebox.FragPhoto.onDetach(FragPhoto.java:117)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:991)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1017)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1826)
09-30 13:50:36.384: W/System.err(8310):     at android.app.Activity.performDestroy(Activity.java:5171)
09-30 13:50:36.384: W/System.err(8310):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3260)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3291)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3489)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.access$700(ActivityThread.java:130)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
09-30 13:50:36.394: W/System.err(8310):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 13:50:36.394: W/System.err(8310):     at android.os.Looper.loop(Looper.java:137)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-30 13:50:36.394: W/System.err(8310):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 13:50:36.394: W/System.err(8310):     at java.lang.reflect.Method.invoke(Method.java:511)
09-30 13:50:36.394: W/System.err(8310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-30 13:50:36.394: W/System.err(8310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 13:50:36.394: W/System.err(8310):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>FragPhoto line 117 is on "((ViewGroup) principale.getParent()).removeAllViews();"</p>

<p>Here is my code for the activity :</p>

<pre><code>/** Called when the activity is first created. */
@Override
public void onCreate(Bundle etatSauve) {
    super.onCreate(etatSauve);
    setContentView(R.layout.mainphoto);
    }

@Override
public void onDestroy () {
    super.onDestroy();
}
</code></pre>

<p>Layout for the activity :</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/toutenhaut"
     android:layout_width="match_parent"
     android:layout_height="match_parent" &gt;

&lt;fragment
     android:id="@+id/activitePhotoFrag"
     android:name="debut.telebox.FragPhoto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>And the code for the fragment contains :</p>

<pre><code>private View principale;
private SimpleCursorAdapter monAdapter;
private AdapterViewFlipper vuele;

@Override
public void onCreate(Bundle etatSauve) {
    super.onCreate(etatSauve);
    this.setHasOptionsMenu(true);
    this.setRetainInstance(true);
    }

@Override
public View onCreateView(LayoutInflater inflateur, ViewGroup container,
        final Bundle etatSauve) {

    if (principale == null) {
        principale = inflateur.inflate(R.layout.fragmentphoto, container, false);
        vuele = (AdapterViewFlipper) principale.findViewById(R.id.photoflipper);

    }

    return principale;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getLoaderManager().getLoader(0) == null)
        getLoaderManager().initLoader(0, getArguments(), this);
}

public void onAttach (Activity activity) {
    super.onAttach(activity);
}

public void onDetach() {
    super.onDetach();
    try {
        ((ViewGroup) principale.getParent()).removeAllViews();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}
</code></pre>

<p>It seems that the fragment view parent is not good the second time ... Why ?</p>
