Androidx Navigation View - `setnavigationitemselectedlistener` Doesn't Work
Solution 1:
I Figured it out guys.
Just in case if someone needs it, I'm posting it here.
Instead of this:
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
returnfalse;
}
});
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
NavigationUI.setupWithNavController(navigationView, navController);
I changed to:
navigationView = findViewById(R.id.navigationView);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
returnfalse;
}
});
And it worked, may be we need to configure everything before attaching the onNavigationSelector
.
Solution 2:
You might not have understood the full side effect of switching the order.
NavigationUI.setupWithNavController(navigationView, navController); // Line 1
navigationView.setNavigationItemSelectedListener({...}) // Line 2
The NavigationUI
internally attaches NavigationView.OnNavigationItemSelectedListener
to the NavigationView
at Line1
. You are overriding that listener with your custom listener in Line 2
.
This means navController
won't work and you have to handle all of the navigation actions manually in your custom listener. So the full solution might be something along the lines of:
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(
newNavigationView.OnNavigationItemSelectedListener() {
@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem menuItem) {
// TODO: do stuff
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
// You need this line to handle the navigationbooleanhandled= NavigationUI.onNavDestinationSelected(menuItem, navController);
if (handled) {
ViewParentparent= navigationView.getParent();
if (parent instanceof DrawerLayout) {
((DrawerLayout) parent).closeDrawer(navigationView);
}
}
return handled;
}
});
Note:
You still might want to call setupWithNavController
before attaching your custom listener because it does things other than attaching navigation item click listener.
Solution 3:
In case that someone still looking for answer how to have both: NavigationController to handle NavigationView items, but also to have some specific action inside of the NavigationView.
In menu.xml file setup menu items as usual:
<menu>
<item
android:id="@+id/fragment_settings"
android:icon="@drawable/ic_settings"
android:orderInCategory="3"
android:title="@string/settings" />
<item
android:id="@+id/share_app"
android:icon="@drawable/ic_share"
android:orderInCategory="4"
android:onClick="shareApp"
android:title="@string/share_to_friends" />
<item
android:id="@+id/fragment_about"
android:icon="@drawable/ic_info"
android:orderInCategory="5"
android:title="@string/about" />
</menu>
First define a onClick method "shareApp" for the "share_app" menu item. Then in your activity create method like this:
funshareApp(item:MenuItem) {
logD("share app clicked!")
val intent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_TEXT, "some text to send...")
type = "text/*"
}
startActivity(Intent.createChooser(intent, "Share app..."))
}
Don't forget to attach a Toolbar, NavigationView and DrawerLayout to NavController in the override fun onCreate(savedInstanceState: Bundle?) method of activity (or where ever you want)
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupActionBarWithNavController(navController, drawer_layout)
setupWithNavController(nav_view, navController)
setupWithNavController(toolbar, navController, drawer_layout)
}
Solution 4:
Try this, this one is perfect solution I hope. Simply write below code to navigate to your fragments.
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
..........
..........
navigationView.setNavigationItemSelectedListener(this);
}
@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
int id = menuItem.getItemId();
switch (id) {
case R.id.first:
navController.navigate(R.id.firstFragment);// **firstFragment** is the id that is written the file under **res/navigation/mobile_navigation.xml** break;
case R.id.second:
navController.navigate(R.id.secondFragment);
break;
case R.id.third:
navController.navigate(R.id.thirdFragment);
break;
}
returntrue;
}
Solution 5:
MainActivity should implement OnNavigationItemSelectedListener, your code should look like
publicclassMainActivityextendsAppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener{
...
}
Post a Comment for "Androidx Navigation View - `setnavigationitemselectedlistener` Doesn't Work"