title: Android with Xamarin
body: <p>I've been struggling for some time on this problem, without solving it or finding a solution over the net.</p>
  
  <p>I've basically an <code>ActionBar</code> with four tabs, handled through <code>Fragments</code>. They contain various parameters, but two of them need to be password protected. My idea was to request the password to the user (I've already a separated activity I use whenever I've to ask for a password), and if it's inserted correctly let the user navigate to the "<em>protected tabs</em>", returning him to the tab selected before the password request if it was not correctly typed.</p>
  
  <p>In <em>Windows</em> this was achieved through the <code>Selecting event</code> of a <code>tab control</code> that has the <code>Cancel property</code>.
  Here's the code I'm using :</p>
  
  <pre><code>    protected override void OnCreate(Bundle bundle)
      {
          base.OnCreate(bundle);
  
          _passwordInserted = false;
          _pwd = this.Intent.GetStringExtra(ExtraValues.Password);
  
          SetContentView(Resource.Layout.Parametri);
  
          ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
  
          AddTab("Stampante", Resource.Drawable.stampa_48, new paramPrinterFragment(), false, 0);
          AddTab("Utility", Resource.Drawable.utility_48, new paramUtilityFragment(), false, 1);
          AddTab("Sync", Resource.Drawable.synchronize, new paramSyncFragment(), true, 2);
          AddTab("Parametri", Resource.Drawable.settings, new paramCommonFragment(), true, 3);
      }
  
      protected override void OnStart ()
      {
          base.OnStart ();
      }
  
      void AddTab (string tabText, int iconResourceId, Fragment view, bool reqPassword, int tabIndex)
      {
          var tab = this.ActionBar.NewTab ();
  
          tab.SetText (tabText);
          tab.SetIcon (iconResourceId);
  
          // must set event handler before adding tab
          tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
          {
              _newTab = tabIndex;
  
              if (reqPassword &amp;&amp; !_passwordInserted)
              {
                  Intent _password = new Intent(this, typeof(dialogPassword));
  
                  if (_pwd == null)
                      _password.PutExtra(ExtraValues.Password, "");
                  else
                      _password.PutExtra(ExtraValues.Password, _pwd.ToString());
  
                  StartActivityForResult(_password, (int)ActivityRequests.Password);
              }
              else
              {
                  _lastTab = tabIndex;
                  Fragment fragment = this.FragmentManager.FindFragmentById(Resource.Id.paramFragmentContainer);
  
                  if (fragment != null)
                      e.FragmentTransaction.Remove(fragment);         
  
                  e.FragmentTransaction.Add(Resource.Id.paramFragmentContainer, view);
              }
          };
  
          tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e)
          {
              e.FragmentTransaction.Remove(view);
          };
  
          this.ActionBar.AddTab (tab);
      }
  
      protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
      {
          if (requestCode != (int)ActivityRequests.Password)
          {
              base.OnActivityResult(requestCode, resultCode, data);
  
              return;
          }
  
          if(clStaticCommon._utilsHandler == null)
              clStaticCommon._utilsHandler = new Handler();
  
          _passwordInserted = resultCode == Result.Ok;
  
          clStaticCommon._utilsHandler.PostAtFrontOfQueue(SelectCallback);
      }
  
      private void SelectCallback()
      {
          ActionBar.SetSelectedNavigationItem(_passwordInserted ? _newTab : _lastTab);
          ActionBar.SelectTab(ActionBar.GetTabAt(_passwordInserted ? _newTab : _lastTab));
  
      }
  </code></pre>
  
  <p>I'm currently working on Android 4.4
  Thanks.
  Rodolfo.</p>
  
  <p>PS: forgot to mention.... This code works, but when you select a protected page for the first time, and insert the correct password, the tab isn't displayed. You have to select another tab and then go back.</p>
  
  <p>PS2: solved modifyng as reported below the TabSelected event. The tab remains visible under the password request dialog, but it's not so important.</p>
  
  <pre><code>        tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
          {
              _newTab = tabIndex;
  
              Fragment fragment = this.FragmentManager.FindFragmentById(Resource.Id.paramFragmentContainer);
  
              if (fragment != null)
                  e.FragmentTransaction.Remove(fragment);         
  
              e.FragmentTransaction.Add(Resource.Id.paramFragmentContainer, view);
  
              if (reqPassword &amp;&amp; !_passwordInserted)
              {
                  Intent _password = new Intent(this, typeof(dialogPassword));
  
                  if (_pwd == null)
                      _password.PutExtra(ExtraValues.Password, "");
                  else
                      _password.PutExtra(ExtraValues.Password, _pwd.ToString());
  
                  StartActivityForResult(_password, (int)ActivityRequests.Password);
              }
              else
                  _lastTab = tabIndex;
          };
  </code></pre>