title: removeAllViews() error when retaining fragment instance
body: <p>I am trying to retain my fragment instance on orientation change. I use the below code in my fragment.</p>
  
  <pre><code>private View fragmentLayoutView;
  private ListView myListView;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      ...
      setRetainInstance(true);
      ...
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  
      if(fragmentLayoutView == null){
          fragmentLayoutView = inflater.inflate(R.layout.myfragmentLayout,container,false);
      }
      else{
          ((ViewGroup) myListView.getParent()).removeAllViews();
      }
      return fragmentLayoutView;
      }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      myListView = (ListView) getActivity().findViewById(R.id.listview1);
      myCustomCursorAdaptor = new MyCustomCursorAdaptor(getActivity());
      myListView.setAdapter(myCustomCursorAdaptor);
      ...
      }
  </code></pre>
  
  <p>But i get an error after <code>onCreateView()</code> has returned. The <code>removeAllViews()</code> line seems to be creating the problem.</p>
  
  <p><strong>Logcat :</strong></p>
  
  <pre><code>06-17 23:57:08.631: E/AndroidRuntime(1149): FATAL EXCEPTION: main
  06-17 23:57:08.631: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.www/com.mypackage.myActionBarActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3895)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.access$800(ActivityThread.java:168)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:137)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:5493)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:525)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)
  06-17 23:57:08.631: E/AndroidRuntime(1149): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.Activity.setContentView(Activity.java:1956)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.mypackage.myActionBarActivity.onCreate(myActionBarActivity.java:20)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.Activity.performCreate(Activity.java:5372)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     ... 12 more
  06-17 23:57:08.631: E/AndroidRuntime(1149): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3739)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addView(ViewGroup.java:3610)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addView(ViewGroup.java:3555)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addView(ViewGroup.java:3531)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1093)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:310)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
  06-17 23:57:08.631: E/AndroidRuntime(1149):     ... 25 more
  </code></pre>
  
  <p>There is no error, but the listView disappears from the layout if i try the below options :</p>
  
  <ul>
  <li>remove the if-else and simply inflate every time</li>
  <li>do a getParent on the fragment layout instead of on the listView - <code>((ViewGroup) fragmentLayoutView.getParent()).removeAllViews();</code>, </li>
  </ul>
  
  <p>What am i doing wrong?</p>