Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 656
Dilli Babu Kadati Avatar asked Nov 20 '25 11:11

Dilli Babu Kadati


1 Answers

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);
    }
like image 71
Genius8020 Avatar answered Nov 22 '25 01:11

Genius8020



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!