i am using a Navigation Drawer with a navigation graph together. Everything works fine but i want to add a share button in the drawer that should only do an action and show the share sheet and NOT open a new fragment.
The issue is when i override the setNavigationItemSelectedListener
method of the NavigationView
to do that, the navigation graph does not work then anymore.
This is my onCreate
method in MainActivity
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.main_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, R.id.nav_evidenceIntroduction, R.id.nav_settings, R.id.nav_video, R.id.fragment_credits, R.id.nav_umlaute, R.id.fragment_favorite), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.nav_share -> {
drawerLayout.closeDrawer(GravityCompat.START)
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.teilen_detail, getString(R.string.app_name), "".addWatermark()))
sendIntent.type = "text/plain"
startActivity(sendIntent)
HapticFeedback.vibrate(it.actionView)
true
}
else -> true
}
}
}
use setOnMenuItemClickListener
instead of setNavigationItemSelectedListener
navView.menu.findItem(R.id.nav_share).setOnMenuItemClickListener {}