0

In BottomNavigationView it is possible to set:

    bottomNavigationView.setOnNavigationItemReselectedListener(item -> {
        // do nothing on reselection
    });

However for NavigationView this is not available. What is a good equivalent?


UPDATE

Thanks to @ande I implemented the following:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    if (currentMenuItemId == item.getItemId()) {
        navDrawer.close();
        return true;
    }
    currentMenuItemId = item.getItemId();
    NavigationUI.onNavDestinationSelected(item, navController);
    navDrawer.close();
    return true;
}

That works well for if I only navigate via the menu items. (Btw, I just implemented the Listener in my Activity and added it from there, no need for an extra class)
However, when I press the back button then I will be able to press the menu button for the current destination, as the menu item did not update on onBackPressed().


Update 2

    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
            currentMenuItemId = destination.getId();
        }
    });

This solved it!

Tobi
  • 858
  • 7
  • 15
  • Why you are saying `NavigationView` doesn't have [setOnNavigationItemReselectedListener](https://developer.android.com/reference/com/google/android/material/navigation/NavigationView#setnavigationitemselectedlistener)?; correct me if I didn't get something – Zain Nov 16 '20 at 22:31
  • @Zain Your link points to `setNavigationItemSelectedListener` and not **RE** selected. Follow my two links and you see the missing interface – Tobi Nov 17 '20 at 07:44
  • @Zain do you have any suggestion? – Tobi Nov 21 '20 at 18:12
  • Are you using a NavController? – Taha Malik Nov 22 '20 at 13:01
  • @TahaMalik yes I do – Tobi Nov 22 '20 at 15:36

1 Answers1

1

May be you can do like this ( I did quickly so may be it could be better!!)

    class ReSelectedListener(val navigationViewCallBack: NavigationViewCallBack) : NavigationView.OnNavigationItemSelectedListener {

    var newItemSelected : MenuItem? = null
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        if(newItemSelected != null){
            if(newItemSelected!!.itemId == item.itemId){
                navigationViewCallBack.setOnNavigationItemReselectedListener(item)
            }
        }
        newItemSelected = item
        return true
    }


}

interface NavigationViewCallBack {
    fun setOnNavigationItemReselectedListener(item: MenuItem)
}

And after

class YourFragment :Fragment(), NavigationViewCallBack

and

navigationView.setNavigationItemSelectedListener(ReSelectedListener(this))

and implements callback method in YourFragment:

 override fun setOnNavigationItemReselectedListener(item: MenuItem) {
        TODO("Not yet implemented")
    }
ande
  • 319
  • 2
  • 14
  • Thanks for the input! That brought me one step further. When clicking a menu item twice this works well. However after pressing the back button, this does not prevent current view to be reloaded as the `newItemSelected` was not updated in onBackPressed. Please see my update above! – Tobi Nov 22 '20 at 11:16
  • @Tobi, if you have your navController, then you can add a addOnDestinationChangedListener on it and after you have a destination id and you can do when(destination.id) { id.toto -> currentItem = yourIdMenu } – ande Nov 22 '20 at 15:48
  • how do I get the `MenuItem` object based on the destination.id ? Is item.id the same as destination.id ? – Tobi Nov 22 '20 at 16:01
  • I solved it! Thanks! item.id = destination.id :) When you update the answer I will accept it – Tobi Nov 22 '20 at 16:12
  • @Tobi, great, yes it was this but be carefull, because may be destination.id is not a menuItem.id ! – ande Nov 22 '20 at 16:16
  • true I have destinations that are no menuItems. But this is ok, because then it will be possible to click every menuItems! And once a menuItem was clicked, it will be blocked again :) – Tobi Nov 22 '20 at 16:18