'onNavigationItemSelected is not called when the selected menu item is same

I have a simple BottomNavigationView with two menu items (Home Fragment, Settings Fragment) in an activity.

I have implemented onNavigationItemSelectedListener and onNavigationItemSelected.

Also bottomNavigationView.setOnNavigationItemSelectedListener(this);

App page lands on the Home Fragment.

onNavigationItemSelected is being called when I switch between menu items but When I first launch the app and tap on the same menu Item i.e. Home Fragment, onNavigationItemSelected is not being called.

I would need to show a toast whenever the user clicks on the home page when user is already in home page but onNavigationItemSelected event is not triggered.



Solution 1:[1]

First of all we do this in the MainActivity

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int v = item.getItemId();
        if(v==R.id.home)
        {
            getSupportActionBar().setTitle("Home");
            Fragment fragment = new HomeFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
        else if (v==R.id.dash_board)
        {
            getSupportActionBar().setTitle("Dashboard");
            Fragment fragment = new DashboardFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
   return true;
    }
};

This is a example here If you want to know in detail then click here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ajeet Kumar