title: How to resume existing Fragment from BackStack
body: <p>I am learning how to use fragments. I have three instances of <code>Fragment</code> that are initialized at the top of the class. I am adding the fragment to an activity like this:</p>
  
  <p>Declaring and initializing:</p>
  
  <pre><code>Fragment A = new AFragment();
  Fragment B = new BFragment();
  Fragment C = new CFragment();
  </code></pre>
  
  <p>Replacing/Adding:</p>
  
  <pre><code>FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  ft.replace(R.id.content_frame, A);
  ft.addToBackStack(null);
  ft.commit();
  </code></pre>
  
  <p>These snippets are working properly. Every fragment is attached to the activity, and is saved to the back stack without any problem.</p>
  
  <p>So when I launch <code>A</code>, <code>C</code>, and then <code>B</code>, the stack looks like this:</p>
  
  <pre><code>| |
  |B|
  |C|
  |A|
  ___
  </code></pre>
  
  <p>And when I press the 'back' button, <code>B</code> is destroyed and <code>C</code> is resumed.</p>
  
  <p>But, when I launch fragment <code>A</code> a second time, instead of resuming from back stack, it is added at the top of the back stack</p>
  
  <pre><code>| |
  |A|
  |C|
  |A|
  ___
  </code></pre>
  
  <p>But I want to resume <code>A</code> and destroy all fragments on top of it (if any). Actually, I just like the default back stack behavior. </p>
  
  <p>How do I accomplish this?</p>
  
  <p>Expected: (<code>A</code> should be resumed and top fragments should be destroyed)</p>
  
  <pre><code>| |
  | |
  | |
  |A|
  ___
  </code></pre>
  
  <p><strong>Edit: (suggested by A--C)</strong></p>
  
  <p>This is my trying code:</p>
  
  <pre><code>private void selectItem(int position) {
          Fragment problemSearch = null, problemStatistics = null;
          FragmentManager manager = getSupportFragmentManager();
          FragmentTransaction ft = manager.beginTransaction();
          String backStateName = null;
          Fragment fragmentName = null;
          boolean fragmentPopped = false;
          switch (position) {
          case 0:
              fragmentName = profile;
              break;
          case 1:
              fragmentName = submissionStatistics;
              break;
          case 2:
              fragmentName = solvedProblemLevel;
              break;
          case 3:
              fragmentName = latestSubmissions;
              break;
          case 4:
              fragmentName = CPExercise;
              break;
          case 5:
              Bundle bundle = new Bundle();
              bundle.putInt("problem_no", problemNo);
              problemSearch = new ProblemWebView();
              problemSearch.setArguments(bundle);
              fragmentName = problemSearch;
              break;
          case 6:
              fragmentName = rankList;
              break;
          case 7:
              fragmentName = liveSubmissions;
              break;
          case 8:
              Bundle bundles = new Bundle();
              bundles.putInt("problem_no", problemNo);
              problemStatistics = new ProblemStatistics();
              problemStatistics.setArguments(bundles);
              fragmentName = problemStatistics;
          default:
              break;
          }
          backStateName = fragmentName.getClass().getName();
          fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
          if (!fragmentPopped) {
              ft.replace(R.id.content_frame, fragmentName);
          }
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
          ft.addToBackStack(backStateName);
          ft.commit();
  
          // I am using drawer layout
          mDrawerList.setItemChecked(position, true);
          setTitle(title[position]);
          mDrawerLayout.closeDrawer(mDrawerList);
      }
  </code></pre>
  
  <p>The problem is, when I launch <code>A</code> and then <code>B</code>, then press 'back', <code>B</code> is removed and <code>A</code> is resumed. and pressing 'back' a second time should exit the app. But it is showing a blank window and I have to press back a third time to close it.</p>
  
  <p>Also, when I launch <code>A</code>, then <code>B</code>, then <code>C</code>, then <code>B</code> again...</p>
  
  <p>Expected:</p>
  
  <pre><code>| |
  | |
  |B|
  |A|
  ___
  </code></pre>
  
  <p>Actual:</p>
  
  <pre><code>| |
  |B|
  |B|
  |A|
  ___
  </code></pre>
  
  <p>Should I override <code>onBackPressed()</code> with any customization or am I missing something?</p>