title: Android back button navigate to specific fragment
body: <p>I have the navigation drawer menu structure presently setup to allow for going back to the previous fragmnet, with addToBackStack, when selecting the back button as:</p>
  
  <pre><code>    if (menuItem.getItemId() == R.id.nav_item_messages) {
              FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
              xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment()).addToBackStack("MainActivity").commit();
          }
  </code></pre>
  
  <p>However, I wish to allow for the back button to always send the user back to a specific framgment, not simply the previous one. </p>
  
  <p>How can this be accomplished? </p>
  
  <p><strong>amended</strong></p>
  
  <p>I attempted adding the code:</p>
  
  <pre><code>int homeFragmentIdentifier = -1;
  </code></pre>
  
  <p><code>...</code></p>
  
  <pre><code>            if (menuItem.getItemId() == R.id.nav_item_messages) {
                  FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                  if(homeFragmentIdentifier == -1) {
                      homeFragmentIdentifier = xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment()).commit();
                  } else {
                      xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment()).commit();
                  }
  
              }
  </code></pre>
  
  <p><code>...</code></p>
  
  <pre><code>@Override
  public void onBackPressed() {
      super.onBackPressed();
  
      getFragmentManager().popBackStack(homeFragmentIdentifier, 0);
  
  }
  </code></pre>
  
  <p>But it simply calls the error: </p>
  
  <pre><code>01-15 19:31:55.368 18410-18410/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: com.example.android.feastappv2, PID: 18410
                                                     java.lang.IllegalArgumentException: Bad id: -1
                                                         at android.app.FragmentManagerImpl.popBackStack(FragmentManager.java:526)
                                                         at com.example.android.feastappv2.feastapp.MainActivity.onBackPressed(MainActivity.java:143)
                                                         at android.app.Activity.onKeyUp(Activity.java:2667)
                                                         at android.view.KeyEvent.dispatch(KeyEvent.java:3214)
                                                         at android.app.Activity.dispatchKeyEvent(Activity.java:2922)
  </code></pre>