Dilli Babu Kadati
Dilli Babu Kadati

Reputation: 169

How stop click on same bottom navigation item?

I've a bottom navigation with 4 menu items.

If the user is in the home navigation fragment and he clicks on the home navigation item again, the fragment is getting recreated.

How do I disable the click for the current navigation menu item?

Here's my code for the navigation:

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_feed, R.id.navigation_profile, R.id.trips_feed).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

Upvotes: 3

Views: 812

Answers (1)

Genius8020
Genius8020

Reputation: 145

You need to manage this with switch case So get the fragment that's now open Then say if the fragment is open, never open again

example code :

Fragment fragment = null;

    List<Fragment> fragments = getSupportFragmentManager().getFragments();

    for (Fragment currentFragment : fragments) {


        switch (item.getItemId()) {

            case R.id.navigation_home:
                if (!(currentFragment instanceof HomeFragmentGeneral)) {
                    fragment = HomeFragmentGeneral.newInstance();
                }
                break;


            case R.id.navigation_search:
                if (!(currentFragment instanceof NearlyFrag)) {
                    fragment = NearlyFrag.newInstance();
                }
                break;

            case R.id.navigation_profile:
                if (!(currentFragment instanceof ProfileFragment)) {
                    fragment = ProfileFragment.newInstance();
                }
                break;
        }
    }


    if (fragment != null) {
        attachFragmentToActivity(fragment, R.id.frame);
    }

Upvotes: 1

Related Questions