title: Android fragment -- OnDestroy being called when switching to a different tab followed by OnCreate when returning to the tab?
body: <p>I have a tabbed Android app.  When I switch away from a tab, I am seeing OnDestroy called for the corresponding fragment.  When I come back to that tab, I am then seeing OnCreate called for THE SAME FRAGMENT OBJECT.  It is definitely the same object, not another instance of the same class.  If I switch away to another tab, OnDestroy is called again, followed by a third call to OnCreate if I return to that tab, and so on.</p>
  
  <p>Android <a href="http://developer.android.com/guide/components/fragments.html" rel="nofollow">docs</a> appear to state that this should not happen.</p>
  
  <p>Does this indicate an architectural problem with my app?  If it's relevant, I am using Mono, and I am setting RetainInstance to true in OnActivityCreated.</p>
  
  <p>EDIT: code below is from the activity that wraps all the tabs.</p>
  
  <pre><code>protected override void OnCreate(Bundle bundle) {
    base.OnCreate(bundle);
    MainContext mainContext = MainContext;
    TabContext tabContext = mainContext.TabContext;
    // Tab is a custom model object; not related to Android tabs.
    List&lt;Tab&gt; tabs = tabContext.Tabs;
    foreach (Tab tab in tabs) {
      string displayString = tab.DisplayString;
      string withUnderscores = displayString.Replace(' ', '_');
      Type fragmentType = Type.GetType(Assembly.GetExecutingAssembly().GetName().Name + "." + withUnderscores + "Fragment");
      this.AddTab(tab, (Fragment) Activator.CreateInstance(fragmentType));
    }
    ActionBar bar = this.ActionBar;
    bar.NavigationMode = ActionBarNavigationMode.Tabs;
    bar.SetDisplayShowTitleEnabled(false);
    bar.SetDisplayShowHomeEnabled(false);
    SetContentView(Resource.Layout.MainTabActivityLayout);
  
    if (bundle != null) {
      int index = bundle.GetInt("index");
      ActionBar.SetSelectedNavigationItem(index);
    }
  }
  
  private void AddTab(Tab tab, Fragment fragment) {
    ActionBar bar = this.ActionBar;
    Android.App.ActionBar.Tab droidTab = bar.NewTab();
  
  
    droidTab.SetTag(tab.ToString());
    // omitting code that sets icon and display text.
    droidTab.TabSelected += (sender, e) =&gt; {
      e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment, tab.ToString());
    };
  
    this.ActionBar.AddTab(droidTab);
  }
  </code></pre>