title: On Navigation Drawer back button is not working correctly?
body: <p><a href="https://i.stack.imgur.com/mNLfl.png" rel="nofollow noreferrer">Fragment Overlap</a></p>
  
  <p>I have 5 fragments.On pressing back button  I want to move to home fragment no matter how many fragments are opened.</p>
  
  <p>If I move from</p>
  
  <p>[1] --> [2] and Press back [1] shown home fragment</p>
  
  <p>[1] --> [3] and Press back [1] shown home fragment</p>
  
  <p>[1] --> [4] and Press back [1] shown home fragment</p>
  
  <p>[1] --> [5] and Press back [1] shown home fragment</p>
  
  <p>As I want No Problem.</p>
  
  <p>But Problem is</p>
  
  <p>[1] --> [2] --> [3] and Press back home fragment overlap on [3]</p>
  
  <p>[1] --> [2] --> [4] and Press back home fragment overlap on [4]</p>
  
  <p>[1] --> [2] --> [5] and Press back home fragment overlap on [5]</p>
  
  <p>I have added only home fragment to backStack and used replace fragment method.</p>
  
  <p>Why this is happening???</p>
  
  <blockquote>
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.all_activity_layout);
      setToolbar();
      addFragment(AttractionsFragment.newInstance());
      setNavigationDrawer();
  }
  
   @SuppressWarnings("StatementWithEmptyBody")
   @Override
   public boolean onNavigationItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.nav_attractions:
              if (mCurrentNavigationDrawerItem != 0) {
                  mCurrentNavigationDrawerItem = 0;
                  replaceFragment(AttractionsFragment.newInstance());
              }
              break;
          case R.id.nav_packages:
              if (mCurrentNavigationDrawerItem != 1) {
                  mCurrentNavigationDrawerItem = 1;
                  replaceFragment(PackagesFragment.newInstance());
              }
              break;
          case R.id.nav_passes:
              if (mCurrentNavigationDrawerItem != 2) {
                  mCurrentNavigationDrawerItem = 2;
                  replaceFragment(PassesFragment.newInstance());
  
              }
              break;
          case R.id.nav_coupons:
              if (mCurrentNavigationDrawerItem != 3) {
                  mCurrentNavigationDrawerItem = 3;
                  replaceFragment(CouponsFragment.newInstance());
  
              }
              break;
          case R.id.nav_more:
              if (mCurrentNavigationDrawerItem != 4) {
                  mCurrentNavigationDrawerItem = 4;
                  replaceFragment(MoreFragment.newInstance());
              }
              break;
      }
  
      mNavigationView.getMenu().getItem(mCurrentNavigationDrawerItem).setChecked(true);
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
  }
  
  public void replaceFragment(Fragment fragment) {
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
      // Get Current Visible fragment
      Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
      // Add to back stack only if it is AttractionsFragment
      if (f instanceof AttractionsFragment) {
          transaction.addToBackStack(fragment.getClass().getName());
      }
  
      transaction.replace(R.id.fragment_container, fragment);
      transaction.commit();
  
      Log.d("Navigation", "BackStack Count:" + getSupportFragmentManager().getBackStackEntryCount());
  
  
  }
  
  public void addFragment(Fragment fragment) {
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
      transaction.add(R.id.fragment_container, fragment, "AttractionsFragment");
      transaction.commit();
  }
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  </code></pre>
  </blockquote>