I am trying work with navigation component.
currently I have navigation view with menu.
I implement onNavigationItemSelected
in navigation view and detect click by item.getItemId()
when there is clicks on specific items, it do something that not navigate to fragment. for example:
switch (item.getItemId()) {
case R.id.nav_share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "shae StayTuned with friends bla bla bla and we can put app url in the google play " +
"if we don't will be sdk";
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
break;
case R.id.nav_disconnect_user:
FirebaseAuth.getInstance().signOut();
break;
}
how I can achieve that with navigation component? thanks!
With the navigation component you don't need to manually implement a OnNavigationItemSelectedListener
since the setupWithNavController()
method connects destinations to menu items matching the android:id
between your navigation graph and menu items.
If you use a custom NavigationItemSelectedListener
you are removing the original listener. In this case you have to call NavigationUI.onNavDestinationSelected()
to trigger the default behavior.
navigationView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.R.id.nav_share -> {
// handle click
true
}
}
//Call the original listener
NavigationUI.onNavDestinationSelected(it, navController)
drawerLayout.closeDrawer(GravityCompat.START)
true
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.