title: Android Back to first fragment in navigationdrawer
body: <p>I have an app which has multiple fragments in NavigationDrawer.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>@Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          // if nav drawer is opened, hide the action items
          boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
          menu.findItem(R.id.action_search).setVisible(!drawerOpen);
          return super.onPrepareOptionsMenu(menu);
      }
  
      /**
       * Diplaying fragment view for selected nav drawer list item
       * */
      private void displayView(int position) {
          // update the main content by replacing fragments
          fragment = null;
  
          switch (position) {
          case 0:
              fragment = new HomeFragment();
              break;
          case 1:
              fragment = new GalleryFragment();
              break;
          case 2:
              fragment = new ContactFragment();
              break;
          default:
              break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .replace(R.id.frame_container, fragment).commit();
  
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
      }
  
      @Override
      public void setTitle(CharSequence title) {
          mTitle = title;
          getActionBar().setTitle(mTitle);
      }
  
      /**
       * When using the ActionBarDrawerToggle, you must call it during
       * onPostCreate() and onConfigurationChanged()...
       */
  
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          // Sync the toggle state after onRestoreInstanceState has occurred.
          mDrawerToggle.syncState();
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          // Pass any configuration change to the drawer toggls
          mDrawerToggle.onConfigurationChanged(newConfig);
  
      }
  
  
      @Override
      public void onBackPressed() {
  
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .add(R.id.frame_container, fragment).commit();
  
              int position = 0;
              fragment = new HomeFragment();
  
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          } 
  
  
  
      } 
  </code></pre>
  
  <p>What it basically does, it goes first to HomeFragment, when I wanna go to the other <code>fragment</code> MediaFragment and decide go back pressing the back button, I want to go to HomeFragment and when I want to close the app pressing again the back button it will now dismiss the app just like the gmail application. How can I achieve that? I'm having an issue using the code I put in my <code>onBackPressed()</code>. Whenever I go to other fragments the HomeFragment is the one that's showing. Any ideas? Your help will be greatly appreciated.</p>
  
  <p><strong>UPDATE</strong>: Before when I was in <code>MediaFragment</code> and pressed the back button it automatically closes the app. It should not be like, but it should go to the <code>HomeFragment</code>, and when it is in <code>HomeFragment</code> and I click back button then it will close the app.</p>