title: Move straight to home fragment when pressing back button with a NavigationDrawer
body: <p>I am having trouble in my NavigationDrawer. I want to move to the 1st fragment when pressing the back button no matter on which fragment I am currently in. I have 4 fragments in the NavigationDrawer. I have also added <code>addToBackStack()</code>, but it traverses through all the fragments I have opened previously. I have seen many questions related to this but none were helpful. Here's my code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
      Fragment fragment = null;
      static Toolbar toolbar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          toolbar = (Toolbar) findViewById(R.id.toolbarMain);
          setSupportActionBar(toolbar);
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.addDrawerListener(toggle);
          toggle.syncState();
  
          FragmentManager fragmentManager = getSupportFragmentManager();
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          fragment = new ContentMainActivity();
          fragmentTransaction.add(R.id.containerView, fragment).commit();
  
          NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
      }
  
      @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
          } else {
              super.onBackPressed();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_about) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @SuppressWarnings("StatementWithEmptyBody")
      @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          int id = item.getItemId();
  
          if (id == R.id.nav_home) {
              fragment = new ContentMainActivity();
          } else if (id == R.id.nav_batches) {
              fragment = new ViewBatches();
          } else if (id == R.id.nav_view_students) {
              fragment = new ViewAllStudents();
          } else if (id == R.id.nav_fee) {
              fragment = new FeePayments();
          }
  
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.containerView, fragment).addToBackStack(null).commit();
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
      ...
  }
  </code></pre>