title: Attaching and Detaching Fragments do not work on Xamarin Android
body: <p>I have 3 tabs under my action bar and I want each menu to show different layouts. After researching for some time, I decided on making each tab fragment and using a FrameLayout as the container of Fragments. I prepared my fragments and tested each seperately and all 3 of my fragments works as intended. But my first approach was causing memory problems because I was not handling the transactions properly. After some more research I found out detaching and attaching is the best option. But when I wrote the detaching and attaching codes, it did not work. First fragment shows up on my screen, when I click on other tab, both detach and attach codes does nothing. The same screen stays on the screen.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>enum eLiveFragment{None,Feed,Articles,Promo}
  
      [Activity (Label = "Live",Theme = "@style/Theme.Discover")]         
      public class LiveActivity : Activity
      {
          FrameLayout contentLayout;
          //FragmentTransaction ft;
  
          FragmentTransaction fragmentTransaction;
          Fragment promoFrag;
          Fragment articleFrag;
          Fragment feedFrag;
  
          protected override void OnCreate (Bundle savedInstanceState)
          {
              base.OnCreate (savedInstanceState);
  
              SetContentView (Resource.Layout.LiveLayout);
  
              ActionBar.Title = Util.GetUIText (UILanguageText.LiveTBC_title);
              ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;       
  
              contentLayout = FindViewById&lt;FrameLayout&gt; (Resource.Id.liveContent);
  
              fragmentTransaction = FragmentManager.BeginTransaction ();
              fragmentTransaction.Commit();
  
              AddTab ("Promo", Resource.Drawable.live_icon_promos,eLiveFragment.Promo);
              AddTab ("Articles", Resource.Drawable.live_icon_articles,eLiveFragment.Articles);
              AddTab ("Feed", Resource.Drawable.live_icon_feed,eLiveFragment.Feed);
          }
  
          void AddTab (string tabText, int iconResourceId,eLiveFragment fragmentType  )
          {
              var tab = this.ActionBar.NewTab ();
              tab.SetCustomView (Resource.Layout.TabLayout);
              ImageView tabImageView = tab.CustomView.FindViewById&lt;ImageView&gt; (Resource.Id.tabImage);
              TextView tabTextView = tab.CustomView.FindViewById&lt;TextView&gt; (Resource.Id.tabText);
              tabImageView.SetImageResource (iconResourceId);
              tabTextView.Text = tabText;  
  
              tab.TabSelected += (sender, e) =&gt; {
  
                  if (fragmentType == eLiveFragment.Promo) 
                  {
                      if (promoFrag == null) 
                      {
                          promoFrag = Fragment.Instantiate(this, typeof(PromoGalleryFragment).Namespace.ToLower() + "." + typeof(PromoGalleryFragment).Name);
                          fragmentTransaction.Add(Resource.Id.liveContent, promoFrag, "Promo");
                      }
                      else
                          fragmentTransaction.Attach(promoFrag);
                  }
                  else if (fragmentType == eLiveFragment.Articles) 
                  {
                      if (articleFrag == null) 
                      {
                          articleFrag = Fragment.Instantiate(this, typeof(ArticleFragment).Namespace.ToLower() + "." + typeof(ArticleFragment).Name);
                          fragmentTransaction.Add(Resource.Id.liveContent, articleFrag, "Article");
                      }
                      else
                          fragmentTransaction.Attach(articleFrag);
                  }
                  else if (fragmentType == eLiveFragment.Feed) 
                  {
                      if (promoFrag == null) 
                      {
                          feedFrag = Fragment.Instantiate(this, typeof(FeedFragment).Namespace.ToLower() + "." + typeof(FeedFragment).Name);
                          fragmentTransaction.Add(Resource.Id.liveContent, feedFrag, "Feed");
                      }
                      else
                          fragmentTransaction.Attach(feedFrag);
                  }
  
              };
  
              tab.TabUnselected += (sender, e) =&gt; {
                  if (fragmentType == eLiveFragment.Promo) 
                  {
                      fragmentTransaction.Detach(FragmentManager.FindFragmentByTag("Promo"));
                  }
                  else if (fragmentType == eLiveFragment.Articles) 
                  {
                      fragmentTransaction.Detach(FragmentManager.FindFragmentByTag("Article"));
                  }
                  else if (fragmentType == eLiveFragment.Feed) 
                  {
                      fragmentTransaction.Detach(FragmentManager.FindFragmentByTag("Feed"));
                  }
              };
              this.ActionBar.AddTab (tab);
          }
      }
  </code></pre>
  
  <p>And here is my XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:minWidth="25px"
      android:minHeight="25px"&gt;
      &lt;FrameLayout
          android:id="@+id/liveContent"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Also I used breakpoints to see if the unselected and selected events works properly, and they do. Attach and detach codes works but does nothing.</p>