title: Android bottom navigation bar when press back go to previous fragment
body: <p>Here is my navigation bar :</p>
  
  <p><a href="https://i.stack.imgur.com/xc8i0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xc8i0.png" alt="Screenshot of navigation bar"></a></p>
  
  <p>There are 4 items in my bottom nav bar , I want it to back to previous fragment when I press the back button,</p>
  
  <p>Here`s my xml :</p>
  
  <pre><code>&lt;android.support.design.widget.BottomNavigationView
      android:id="@+id/bottomnav"
      android:layout_width="382dp"
      android:layout_height="52dp"
      android:layout_marginBottom="0dp"
      app:itemBackground="@color/backbeyez"
      app:itemIconTint="@drawable/nav_items_color"
      app:itemTextColor="@drawable/nav_items_color"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:menu="@menu/navigation"
      tools:layout_editor_absoluteY="515dp"&gt;
  
  &lt;/android.support.design.widget.BottomNavigationView&gt;
  </code></pre>
  
  <p>Here`s the code for MainActivity :</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  public void switchorders() {
      FragmentManager manager = getSupportFragmentManager();
      manager.beginTransaction().replace(R.id.container, new OrdersLayout()).commit();
  }
  
  public void switchcredits() {
      FragmentManager manager = getSupportFragmentManager();
      manager.beginTransaction().replace(R.id.container, new CreditLayout()).commit();
  }
  
  public void switchworks() {
      FragmentManager manager = getSupportFragmentManager();
      manager.beginTransaction().replace(R.id.container, new ListLayout()).commit();
  }
  
  public void switchprofile() {
      FragmentManager manager = getSupportFragmentManager();
      manager.beginTransaction().replace(R.id.container, new ProfileLayout()).commit();
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      switchorders();
  
  
      BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomnav);
      BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
  
  
      bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
          @Override
          public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  
              switch (item.getItemId()) {
                  case R.id.navigation_orders:
                      switchorders();
                      break;
  
                  case R.id.navigation_credit:
                      switchcredits();
                      break;
  
                  case R.id.navigation_works:
                      switchworks();
                      break;
  
                  case R.id.navigation_profile:
                      switchprofile();
                      break;
              }
              return true;
          }
      });
  
  }
  </code></pre>
  
  <p>I also tried defining a back button with the statements below but they didn`t work :</p>
  
  <pre><code>public void aboutback(View view) {
      if (getSupportFragmentManager().getBackStackEntryCount() &gt; 0)
      {
          boolean done = getSupportFragmentManager().popBackStackImmediate();
      }
  }
  </code></pre>
  
  <p>and also "popbackstack()" and "popbackstackImmediate()" did not work!</p>