简体   繁体   中英

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);

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM