Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 233
mohammed speed Avatar asked Sep 16 '25 15:09

mohammed speed


1 Answers

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

like image 85
Rahul Avatar answered Sep 18 '25 08:09

Rahul