title: BottomNavigationView Back button not working properly
body: <p><a href="https://i.stack.imgur.com/5uKXw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5uKXw.png" alt="enter image description here"></a></p>
  
  <p>When is press back button my fragment is changing to home page, but bottom icon is not changing. I posted my all code here. If i select more then two navigation button from bottom navigation view items then when i press back button it'll redirect to last selected button item.</p>
  
  <p>But now what happening is that suppose i'm on the last item say 'Notification' [as you can see in screenshot], now when i press back button it'll directly take me to 'Home' button, but what i want is that it should take me first to 'Search' button item and then to 'Home' not directly to 'home'.</p>
  
  <p>How can i achieve this??</p>
  
  <p>Here is the layout:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
              &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  xmlns:design="http://schemas.android.com/apk/res-auto"
                  android:id="@+id/activity_main"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  tools:context="com.segunfamisa.sample.bottomnav.MainActivity"&gt;
  
                  &lt;FrameLayout
                      android:id="@+id/container"
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="1"
                      android:background="#f1f1f1"&gt;
  
                  &lt;/FrameLayout&gt;
  
                  &lt;android.support.design.widget.BottomNavigationView
                      android:id="@+id/navigation"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="start"
                      design:menu="@menu/bottom_nav_items" /&gt;
              &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is the activity:</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity {
      private static final String SELECTED_ITEM = "arg_selected_item";
  
      private BottomNavigationView mBottomNav;
      private int mSelectedItem;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
          mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                  selectFragment(item);
                  return true;
              }
          });
  
          MenuItem selectedItem;
          if (savedInstanceState != null) {
              mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
              selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
          } else
          {
              selectedItem = mBottomNav.getMenu().getItem(0);
          }
  
          selectFragment(selectedItem);
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          outState.putInt(SELECTED_ITEM, mSelectedItem);
          super.onSaveInstanceState(outState);
      }
  
      @Override
      public void onBackPressed() {
          MenuItem homeItem = mBottomNav.getMenu().getItem(0);
  
          if (mSelectedItem != homeItem.getItemId()) {
              // select home item
             // homeItem.setCheckable(true);
  
              Log.d("backpressids","****  "+mSelectedItem);
              selectFragment(homeItem);
  
          } else {
  
              super.onBackPressed();
  
          }
      }
  
      private void selectFragment(MenuItem item) {
          Fragment frag = null;
          item.setCheckable(true);
          // init corresponding fragment
          switch (item.getItemId()) {
              case R.id.menu_home:
  
                  TabFragmentOne fragmentone = new TabFragmentOne();
                  FragmentTransaction ft =  getSupportFragmentManager().beginTransaction();
                  ft.replace(R.id.container, fragmentone);
                  ft.commit();
  
  
                  break;
              case R.id.menu_notifications:
  
  
                  TabFragmentThree fragmentone_Three = new TabFragmentThree();
                  FragmentTransaction ft_three =  getSupportFragmentManager().beginTransaction();
                  ft_three.replace(R.id.container, fragmentone_Three);
                  ft_three.commit();
  
                  break;
              case R.id.menu_search:
  
  
                  TabFragmentTwo tabFragmentTwo = new TabFragmentTwo();
                  FragmentTransaction ft_two =  getSupportFragmentManager().beginTransaction();
                  ft_two.replace(R.id.container,tabFragmentTwo);
                  ft_two.commit();
  
  
                  break;
          }
  
          // update selected item
          mSelectedItem = item.getItemId();
  
  
          updateToolbarText(item.getTitle());
  
          if (frag != null) {
              TabFragmentOne fragmentone = new TabFragmentOne();
              FragmentTransaction ft =  getSupportFragmentManager().beginTransaction();
              ft.replace(R.id.container, fragmentone);
              ft.commit();
          }
      }
  
      private void updateToolbarText(CharSequence text) {
          ActionBar actionBar = getSupportActionBar();
          if (actionBar != null) {
              actionBar.setTitle(text);
          }
      }
  
  
  }
  </code></pre>
  
  <p>please help me any one.....</p>