title: Navigation drawer, handling the back button to go to previous fragments?
body: <p>I'm using the built-in navigation drawer to run my app. I can't quite figure out how to handle the back button. When it's pressed I want it to load the very first fragment again. Fragment1.</p>
  <p>So when the app launches you see Fragment1 launched. They can then click on Fragment 2-5 to go to other pages. Within all of these pages, I want the back button to take the user back to Fragment1. The only place the user should be able to exit the app via the back button is Fragment1.</p>
  <p>Since it's all handled by a FragmentActivity I tried messing with the back button there. I keep getting a force close error, however:</p>
  <pre><code>(01-11 14:09:33.114: E/AndroidRuntime(8292): android.view.InflateException: Binary XML file line #7: Error inflating class fragment)
  </code></pre>
  <p>This is what I have so far:</p>
  <p>I've made sure to add the fragments to the back stack like this:</p>
  <pre><code>fm.beginTransaction().replace(R.id.main, newFragment).addToBackStack(&quot;fragBack&quot;).commit();
  </code></pre>
  <p>Back button:</p>
  <pre><code>@Override
  public void onBackPressed() {
      if (getSupportFragmentManager().findFragmentByTag(&quot;fragBack&quot;) != null) {
          
      }
      else {
          super.onBackPressed();
          return;
      }
      if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
          Toast.makeText(getApplicationContext(), &quot;Test&quot;, Toast.LENGTH_LONG).show();
          Fragment frag = getSupportFragmentManager().findFragmentByTag(&quot;fragBack&quot;);
          FragmentTransaction transac = getSupportFragmentManager().beginTransaction().remove(frag);
                  transac.commit();
      }
      
  }
  </code></pre>
  <p>Does anyone know what I need to do? Do I need to call onBackPressed in every fragment (if that's even possible) rather than the FragmentActivity that controls the drawer? In my past apps I've been OK with the back button closing the app regardless of which Fragment the user is on but the one I'm making now I want the back button to go back to Fragment1.</p>
  <p>Would really appreciate some help, thank you.</p>
  <p>onItemClick</p>
  <pre><code>@Override
          public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
              
              Fragment newFragment = new MapsPage();
              FragmentManager fm = getSupportFragmentManager();
              switch(i) {
              case 0:
                  newFragment = new Fragment2();
                  break;
              case 1:
                  newFragment = new Fragment3();
                  break;
              case 2:
                  newFragment = new Fragment4();
                  break;
              case 3:
                  newFragment = new Fragment5();
                  break;
              }
              fm.beginTransaction().add(R.id.main, newFragment).addToBackStack(&quot;fragback&quot;).commit();
              drawerLayout.closeDrawer(rl);
          }
  </code></pre>