title: How to smoothly go back to the first fragment on backpress
body: <p>I am using navigation drawer in my app, each item in the drawer starts a fragment. If a fragment is in view, and the back button is pressed I want the activity to start the first fragment.</p>
  
  <p>To expatiate, If I am looking at <strong>AbtFragment</strong> or <strong>ContaFragment</strong> and the back button is pressed, I want the activity to start or go back to <strong>HomeFragment</strong>.</p>
  
  <p><strong>Part of my Activity</strong></p>
  
  <pre><code>public boolean onNavigationItemSelected(MenuItem item) {
          // Handle navigation view item clicks here.
          id = item.getItemId();
  
          Fragment fragment = null;
  
          if (id == R.id.menu_home) {
              fragment = new HomeFragment();
          }
  
          if (id == R.id.about_blog) {
              fragment = new AbtFragment();
  
  
          } else if (id == R.id.nav_contri) {
              fragment = new ContFragment();
  
          } else if (id == R.id.nav_contact) {
              fragment = new ContaFragment();
  
          } else if (id == R.id.nav_privy) {
              fragment = new PageFragment();
  
          } else if (id == R.id.nav_advert) {
              fragment = new PageFragment();
  
          }
  
          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
          ft.replace(R.id.content_frame, fragment, "visible_fragment");
          ft.addToBackStack(null);
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
          ft.commit();
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
  
      @Override
          public void onBackPressed() {
              DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
              if (drawer.isDrawerOpen(GravityCompat.START)) {
                  drawer.closeDrawer(GravityCompat.START);
              } else {
                  super.onBackPressed();
              }
              if (getSupportFragmentManager().getBackStackEntryCount() &gt;= 1) {
                  navigationView.setCheckedItem(R.id.menu_home);
                  navigationView.getMenu().performIdentifierAction(R.id.menu_home, 0);
              }
  
          }
  </code></pre>
  
  <p>From <code>onBackPressed</code>, I am checking for the number of fragment in backstack and then starting <strong>HomeFragment</strong> if it's greater than or equals to one, but this is not working properly. 
  For example, If I start <strong>AbtFragment</strong>, and then <strong>ContFragment</strong>, and I press the back button, <strong>AbtFragment</strong> will first flicker on the screen before  <strong>HomeFragment</strong> will be shown.</p>
  
  <p>Please, how do I correct this?</p>
  
  <p><strong>Note</strong> : <strong>HomeFragment</strong> is the default frgament hence it's started by default when the activity is launched.</p>