title: How to properly remove retained instance Fragment
body: <p>Currently, I would like to retain an expensive data structure, during configuration changes. I choose not to use <code>Bundle</code> to handle it, as the expensive data structure is not parcelable.</p>
  
  <p>Hence, I use a non-UI Fragment (Called it <strong>RetainInstanceFragment</strong>), with its <code>setRetainInstance(true)</code> to hold the data structure. </p>
  
  <pre><code>public class RetainInstanceFragment extends Fragment {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // Creating expensive data structure
          expensiveDataStructure = CreateExpensiveDataStructure();
  
          // Tell the framework to try to keep this fragment around
          // during a configuration change.
          setRetainInstance(true);
      }
  
      public ExpensiveDataStructure expensiveDataStructure = null;
  }
  </code></pre>
  
  <p>An UI Fragment (Called it <strong>UIFragment</strong>) will get the expensive data structure from <code>RetainInstanceFragment</code>. Whenever there is configuration changes on <code>UIFragment</code>, <code>UIFragment</code> will always try to get the "cached" <code>RetainInstanceFragment</code> from <code>FragmentManager</code>, before it decides to create a new <code>RetainInstanceFragment</code>. </p>
  
  <p>Example code is as follow.</p>
  
  <pre><code>public class UIFragment extends SherlockListFragment
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          FragmentManager fm = getFragmentManager();
  
          // Check to see if we have retained the worker fragment.
          retainInstanceFragment = (RetainInstanceFragment)fm.findFragmentByTag("data");
  
          // If not retained (or first time running), we need to create it.
          if (retainInstanceFragment == null) {
              retainInstanceFragment = new RetainInstanceFragment();
              fm.beginTransaction().add(watchlistArrayFragment, "data").commit();
          } else {
              // We can re-use retainInstanceFragment.expensiveDataStructure even
              // after configuration change.
          }
      }
  }
  </code></pre>
  
  <p>However, there's a problem. Whenever I destroy my old <code>UIFragment</code>, and replace it with new <code>UIFragment</code>, <strong>I expect old <code>RetainInstanceFragment</code> will be destroyed as well.</strong> Here is how I destroy and create new <code>UIFragment</code></p>
  
  <pre><code>public class MyFragmentActivity extends SlidingFragmentActivity    
      // Being triggered when there is different menu item in sliding menu being
      // selected.
      public void selectActiveContent(Country country) {
          Fragment fragment = new UIFragment(country);
          getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commitAllowingStateLoss();
      }
  </code></pre>
  
  <p><strong>But old <code>RetainInstanceFragment</code> is never destroyed.</strong></p>
  
  <p>My guess is, perhaps I forget to perform clean up in <code>UIFragment</code>. Hence, I add the following code</p>
  
  <h2>UIFragment</h2>
  
  <pre><code>@Override
  public void onDetach() {
      super.onDetach();
      // To differentiate whether this is a configuration changes, or we are
      // removing away this fragment?
      if (this.isRemoving()) {
          FragmentManager fm = getFragmentManager();
          fm.beginTransaction().remove(retainInstanceFragment).commit();
      }
  }
  </code></pre>
  
  <p>However, it doesn't work all the time. I perform several sliding menu clicks.</p>
  
  <pre><code>1. selectActiveContent() -&gt; Create new UIFragment and new RetainInstanceFragment
  2. selectActiveContent() -&gt; Create new UIFragment, but re-use previous RetainInstanceFragment. (Wrong behavior)
  3. selectActiveContent() -&gt; Create new UIFragment, and new RetainInstanceFragment.
  4. selectActiveContent() -&gt; Create new UIFragment, but re-use previous RetainInstanceFragment. (Wrong behavior)
  </code></pre>
  
  <p>Any idea how I can properly remove retained instance Fragment?</p>