Search code examples
androiduinavigationcontrollerandroid-jetpackandroid-jetpack-navigation

Can I use setupWithNavController and setOnNavigationItemSelectedListener at the same time


I have an activity with BottomNavigationView with 5 items and fragment to show the pages, I set the NavController as following : NavigationUI.setupWithNavController(mBottomNavigationView, navController);

but there is one item in the bottom navigation in some Scenario will open another activity.

I tried to add setOnNavigationItemSelectedListener and handle it but, the navigation for the other items doesn't work.

can anyone help me ?


Solution

  • Instead of setOnNavigationItemSelectedListener use addOnDestinationChangedListener. navController.addOnDestinationChangedListener((controller, destination, arguments) -> { if(destination.getId()==R.id.menu_item_id){ //navigate to other activity } });