title: Pop multiple fragments from stack
body: <p>I am writing an application with Xamarin.Android and MvvmCross, but this question applies for native Android in general. I want to be able to declare a root fragment, so that I can later pop off all fragments on top of it on the stack. For example, if I have the fragments:</p>
  
  <p>A|B|C|D</p>
  
  <p>And I am currently on fragment D, I want to be able to pop off fragments B, C and D instantly to get back to fragment A. I am trying to do this with the <code>CurrentFragmentManager.PopBackStack</code> method in my view presenter, but it's not working as I expect it to. Here is my implementation:</p>
  
  <pre><code>public class ViewPresenter : MvxAppCompatViewPresenter
  {
      private int _rootFragmentId;
      private int _currentFragmentId;
  
      public ViewPresenter(IEnumerable&lt;Assembly&gt; androidViewAssemblies) : base(androidViewAssemblies)
      {
          AddPresentationHintHandler&lt;SetRootHint&gt;(SetRootHintHandler);
          AddPresentationHintHandler&lt;PopToRootHint&gt;(PopToRootHintHandler);
      }
  
      protected override void ShowFragment(Type view, MvxFragmentPresentationAttribute attribute, MvxViewModelRequest request)
      {
          base.ShowFragment(view, attribute, request);
  
          // Keep track of the ID of the current fragment (the ID that is given in the Resource)
          _currentFragmentId = attribute.FragmentContentId;
      }
  
      private bool SetRootHintHandler(SetRootHint hint)
      {
          _rootFragmentId = _currentFragmentId;
  
          return true;
      }
  
      private bool PopToRootHintHandler(PopToRootHint hint)
      {
          // This doesn't do anything
          CurrentFragmentManager.PopBackStack(_rootFragmentId, 0);
  
          return true;
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Subrato M.'s idea works for me. Here is my C# implementation for Xamarin developers:</p>
  
  <pre><code>public class ViewPresenter : MvxAppCompatViewPresenter
  {
      private string _rootFragmentName;
      private string _currentFragmentName;
  
      public ViewPresenter(IEnumerable&lt;Assembly&gt; androidViewAssemblies) : base(androidViewAssemblies)
      {
          AddPresentationHintHandler&lt;SetRootHint&gt;(SetRootHintHandler);
          AddPresentationHintHandler&lt;PopToRootHint&gt;(PopToRootHintHandler);
      }
  
      protected override void ShowFragment(Type view, MvxFragmentPresentationAttribute attribute, MvxViewModelRequest request)
      {
          base.ShowFragment(view, attribute, request);
  
          _currentFragmentName = view.FullName;
      }
  
      private bool SetRootHintHandler(SetRootHint hint)
      {
          _rootFragmentName = _currentFragmentName;
  
          return true;
      }
  
      private bool PopToRootHintHandler(PopToRootHint hint)
      {
          while (CurrentFragmentManager.BackStackEntryCount &gt; 0 &amp;&amp;
                 CurrentFragmentManager.GetBackStackEntryAt(CurrentFragmentManager.BackStackEntryCount-1).Name.ToLowerInvariant() != _rootFragmentName.ToLowerInvariant())
          {
              CurrentFragmentManager.PopBackStackImmediate();
          }
  
          return true;
      }
  }
  </code></pre>