title: MvvmCross clear back stack and change TabBarController select tab
body: <p>I have a <code>MvxTabBarViewController</code> instance <code>mainView</code>, and <code>mainView</code> has four tabs, each tab has a <code>UINavigationController</code>. Now I'm in the position like this:</p>
  
  <pre><code>Tab1 -&gt; UINavigationController -&gt; HomeView -&gt; FirstView -&gt; SecondView -&gt; ...
  </code></pre>
  
  <p>I'm in the SecondView and I want to clear the back stack in Tab1, then change TabBarViewController's <code>selectedViewController</code>.</p>
  
  <p>My Code:</p>
  
  <pre><code>if (clearHint.Body.Data["NavigationMode"] == "IncomeView")
  {
      mainView.SelectedViewController = mainView.ViewControllers[2];
      return;
  }
  else if (clearHint.Body.Data["NavigationMode"] == "ClearStack")
  {
      var navController = mainView.ViewControllers[0] as UINavigationController;
      foreach (var item in navController.ViewControllers)
      {
           if (item is HomeView)
           {
               navController.PopToViewController(item, false);
               return;
           }
       }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>var clearStack = new ClearHistoryPresentationHint(new MvxBundle(new Dictionary&lt;string,string&gt;{{ "NavigationMode", "ClearStack" }}));
  ChangePresentation(clearStack);
  
  await Task.Delay(50);
  
  var bundle = new MvxBundle(new Dictionary&lt;string, string&gt; { { "NavigationMode", "IncomeView" } });
  var toInCome = new ClearHistoryPresentationHint(bundle);
  ChangePresentation(toInCome);
  </code></pre>
  
  <p>If I remove this line <strong>await Task.Delay(50);</strong>. App will crash or not work depend on the <em>if/else if</em> order. The problem is I can not put the <em>clear back stack code</em> and <em>change tabbar <code>selectedViewController</code> code</em> all together.</p>
  
  <p>So, what is the correct way to achieve this? </p>
  
  <p><strong>Update 1</strong></p>
  
  <p>No problem use below code without <strong>await Task.Delay(50)</strong>.</p>
  
  <p>If I change the order <em>1,2</em> to <em>2,1</em>, app crash.</p>
  
  <pre><code>if (clearHint.Body.Data["NavigationMode"] == "IncomeView")
  {
      // 1. Change Tab
      mainView.SelectedViewController = mainView.ViewControllers[2];
  
      // 2. Clear BackStack
      var navController = mainView.ViewControllers[0] as UINavigationController;
  
      foreach (var item in navController.ViewControllers)
      {
          if (item is HomeView)
          {
              navController.PopToViewController(item, false);
              break;
          }
      }
      return;
   }
  </code></pre>