title: xamarin android fragments overlapping
body: <p>I know there are a lot of questions already discussing this issue but I still can't figure out what is wrong with my implementation.</p>
  
  <p>I'm using a SherlockFragmentActivity with ActionBar that has 4 tabs, each tab fills the FrameLayout with a different Fragment.</p>
  
  <p>Up to here I have no problem, the tabs switch between fragments perfectly.</p>
  
  <p>But on Tab "Buy" when the searchButton is clicked I replace the FrameLayout with another ListFragment using the Activity1 NavigateTo method, which results in overlapping the current Fragment and won't disappear no matter what I do from this point.</p>
  
  <p>Please advise, I have read all the solution and can't find the problem, let me know if another part of the code is required</p>
  
  <p>Here is my code:
  Activity1:</p>
  
  <pre><code>public class Activity1 : SherlockFragmentActivity, ActionBar_Sherlock.App.ActionBar.ITabListener
      {
  protected override void OnCreate(Bundle bundle)
          {
              base.OnCreate(bundle);
  
              userid = 2;
  
              // Set our view from the "main" layout resource
              SetContentView(Resource.Layout.Main);
              SupportActionBar.NavigationMode = (int)ActionBarNavigationMode.Tabs;
              SupportActionBar.SetDisplayShowTitleEnabled(true);
              //Set the mobile service Sale table adapter
              //this.adapter = new SalesAdapter(this.MobileServiceContext.SaleTable, Resource.Layout.OffersListItem, this);
  
              //Initialize tabs
              Tab tab = SupportActionBar.NewTab();
              tab.SetTag("MyProfile");
              tab.SetText("My Profile");
              tab.SetTabListener(this);
              SupportActionBar.AddTab(tab);
  
              tab = SupportActionBar.NewTab();
              tab.SetTag("Buy");
              tab.SetText("Buy");
              tab.SetTabListener(this);
              SupportActionBar.AddTab(tab);
  
              tab = SupportActionBar.NewTab();
              tab.SetTag("Sell");
              tab.SetText("Sell");
              tab.SetTabListener(this);
              SupportActionBar.AddTab(tab);
  
              tab = SupportActionBar.NewTab();
              tab.SetTag("Rates");
              tab.SetText("Rates");
              tab.SetTabListener(this);
              SupportActionBar.AddTab(tab);
  
          }
  
          public void OnTabSelected(Tab tab, Android.Support.V4.App.FragmentTransaction ft)
          {
              string tag = tab.Tag.ToString();
  
              Android.Support.V4.App.Fragment f = SupportFragmentManager.FindFragmentByTag(tag);
              if (f != null)
              {
                  ft.Show(f);
                  return;
              }
  
              if (tag == "MyProfile")
                  f = new Fragments.MyProfileFragment();
              else if (tag == "Buy")
                  f = new Fragments.BuyFragment();
              else if (tag == "Sell")
                  f = new Fragments.SaleFragment();
              else if (tag == "Rates")
                  f = new Fragments.BuyFragment();
  
  
  
              ft.Add(Resource.Id.fragmentContainer, f, tag);
          }
  
  
          public void OnTabUnselected(Tab tab, Android.Support.V4.App.FragmentTransaction ft)
          {
              string tag = tab.Tag.ToString();
  
              Android.Support.V4.App.Fragment f = SupportFragmentManager.FindFragmentByTag(tag);
              if (f != null)
              {
                  ft.Hide(f);
                  return;
              }
          }
  
          public void OnTabReselected(Tab tab, Android.Support.V4.App.FragmentTransaction ft)
          {
              //Do nothing
          }
  
          public void NavigateTo(Android.Support.V4.App.Fragment newFragment)
          {
              Android.Support.V4.App.FragmentManager manager = SupportFragmentManager;
              Android.Support.V4.App.FragmentTransaction ft = manager.BeginTransaction();
  
              ft.Replace(Resource.Id.fragmentContainer, newFragment);
              ft.SetTransition((int)FragmentTransit.FragmentFade);
              // Add this trnasaction to the back stack, so when the user press back,
              // it rollbacks.
              ft.AddToBackStack(null);
              ft.Commit();
          }
  
          public override void OnBackPressed()
          {
              Android.Support.V4.App.FragmentManager manager = SupportFragmentManager;
              if (manager.BackStackEntryCount &gt; 0)
              {
                  base.OnBackPressed();
              }
              else
              {
  
              }
          }
  }
  </code></pre>
  
  <p>This is the Main layout:</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"&gt;
      &lt;FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is the BuyFragment:</p>
  
  <pre><code>public class BuyFragment : Android.Support.V4.App.Fragment
      {
          private Activity1 myActivity;
          private string currency;
          private int amount;
          private EditText buyAmount;
          private Button searchButton;
          private Spinner currencySpinner;
  
          public override void OnCreate(Bundle savedInstanceState)
          {
              base.OnCreate(savedInstanceState);
              myActivity = (Activity1)this.Activity;
          }
  
          public override void OnActivityCreated(Bundle savedInstanceState)
          {
              base.OnActivityCreated(savedInstanceState);
              //Init the currency spinner
              currencySpinner = this.Activity.FindViewById&lt;Spinner&gt;(Resource.Id.spinnerBuy);
              myActivity.InitCurrencySpinner(currencySpinner);
  
              //Parse the amount text to int
              buyAmount = this.Activity.FindViewById&lt;EditText&gt;(Resource.Id.buyEdittext);
              buyAmount.AfterTextChanged += (sender, args) =&gt;
              {
                  //
                  int result;
                  int.TryParse(buyAmount.Text, out result);
  
                  amount = result;
              };
  
              searchButton = this.Activity.FindViewById&lt;Button&gt;(Resource.Id.buyButton);
              searchButton.Click += (sender, e) =&gt;
              {
                  OnClickSearch(sender, e);
              };
  
              //Disable all buttons if the adapter is updating
              myActivity.SalesAdapter.IsUpdatingChanged += (s, e) =&gt;
              {
                  this.buyAmount.Enabled =
                  this.currencySpinner.Enabled =
                  this.searchButton.Enabled =
                      !myActivity.SalesAdapter.IsUpdating;
              };
          }
  
          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
          {
              return inflater.Inflate(Resource.Layout.BuyLayout, container, false);
          }
  
          private void OnClickSearch(object sender, EventArgs eventArgs)
          {
              try
              {
                  if (amount &gt; 0)
                  {
                      currency = currencySpinner.SelectedItem.ToString();
                      SearchOffers();
                      this.buyAmount.Text = null;
                  }
                  else
                  {
                      Toast.MakeText(this.Activity, "Amount must be a number larger than 0", ToastLength.Short).Show();
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine("SaleFragment.OnClickPutForSale: {0} Exception caught.", e.InnerException);
              }
          }
  
          private void SearchOffers()
          {
              //Check what fragment is shown, replace if needed.
              var fragmentContainer = FragmentManager.FindFragmentById(Resource.Id.fragmentContainer) as SearchListFragment;
              if (fragmentContainer == null)
              {
                  // Make new fragment to show this selection.
                  fragmentContainer = SearchListFragment.NewInstance(amount, currency);
  
                  // Execute a transaction, replacing any existing
                  // fragment with this one inside the frame.
                  myActivity.NavigateTo(fragmentContainer);
              }
              //else
              //{
              //    fragmentContainer.Arguments.PutString("currency", currency);
              //    fragmentContainer.Arguments.PutInt("amount", amount);
              //    myActivity.NavigateTo(fragmentContainer);
              //}
          }
  </code></pre>
  
  <p>Buy layout:</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:padding="10dip"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"&gt;
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="@string/currency_prompt"
          android:layout_marginBottom="10dp" /&gt;
      &lt;Spinner
          android:id="@+id/spinnerBuy"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:prompt="@string/currency_prompt"
          android:layout_marginTop="10dp"
          android:layout_marginBottom="10.0dp" /&gt;
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="@string/amount_prompt"
          android:layout_marginBottom="10dp" /&gt;
      &lt;EditText
          android:id="@+id/buyEdittext"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:layout_marginBottom="10dp"
          android:inputType="number" /&gt;
      &lt;Button
          android:text="@string/BuyButton"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/buyButton"
          android:layout_marginTop="10dp"
          android:layout_marginBottom="10dp" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>