25

I used BottomNavigationView to switch fragments. How to get currently selected menu item, to prevent reopening fragment ?

        BottomNavigationView bottomNavigationView = (BottomNavigationView)
                findViewById(R.id.bottom_navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.action_1:
                                // open fragment 1
                                break;
                            case R.id.action_2:
                                // open fragment 2
                                break;
                            case R.id.action_3:
                                // open fragment 3
                                break;
                        }
                        return false;
                    }
                });
    }
phnmnn
  • 12,813
  • 11
  • 47
  • 64
  • Can you be more clear about ' prevent reopening fragment'? What do you expect on Navigation Item (Menu) click? – Suhayl SH Jan 04 '17 at 08:17

4 Answers4

28

Get selected item at first and then getMenu().findItem(int itemId)

bottomNavigationView.getMenu().findItem(bottomNavigationView.getSelectedItemId())
Ghasem
  • 14,455
  • 21
  • 138
  • 171
Kirill Vashilo
  • 1,559
  • 1
  • 18
  • 27
11

Solution:

private int getSelectedItem(BottomNavigationView bottomNavigationView) {
    Menu menu = bottomNavigationView.getMenu();
    for (int i = 0; i < bottomNavigationView.getMenu().size(); i++) {
        MenuItem menuItem = menu.getItem(i);
        if (menuItem.isChecked()) {
            return menuItem.getItemId();
        }
    }
    return 0;
}
logoff
  • 3,347
  • 5
  • 41
  • 58
phnmnn
  • 12,813
  • 11
  • 47
  • 64
9

Get the currently selected menu item ID using getSelectedItemId:

int selectedItemId = bottomNavigationView.getSelectedItemId();
MenuItem selectedItem = bottomNavigationView.getMenu().findItem(selectedItemId);

This method started being available from Android Support Library 25.3.0.

user5968678
  • 2,074
  • 1
  • 15
  • 17
2

I think the simplest solution for checking if the previous item is the next item is this:

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);

bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            final int previousItem = bottomNavigationView.getSelectedItemId();
            final int nextItem = item.getItemId();
            if (previousItem != nextItem) {
                 switch (nextItem) {
                     case R.id.action_1:
                         // open fragment 1
                         break;
                     case R.id.action_2:
                         // open fragment 2
                         break;
                     case R.id.action_3:
                         // open fragment 3
                         break;
                 }
            }
            return true;
        }
    }
);

Note that there is no need for iterating, and that onNavigationItemSelected returns true, because the function consumes the event.

I hope it helps someone.

XME
  • 515
  • 1
  • 6
  • 18