我正在使用导航体系结构组件,我想从抽屉中打开共享意图吗?
MainActivity类:AppCompatActivity(){
/* override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_share_app -> {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(sendIntent)
return true
}
}
return true
}
* / 重写fun onCreate(savedInstanceState:Bundle?){
setTheme(R.style.AppTheme_NoActionBar)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// navigationView.setNavigationItemSelectedListener(this) setupNavigation()
}
private fun setupNavigation() {
val navController = findNavController(this, R.id.nav_host_fragment)
// Update action bar to reflect navigation
setupActionBarWithNavController(this, navController, drawerLayout)
// Handle nav drawer item clicks
ActivityNavigator(this)
.createDestination().intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
navigationView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.action_share_app -> {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(sendIntent)
}
}
menuItem.isChecked = true
drawerLayout.closeDrawers()
false
}
// Tie nav graph to items in nav drawer
setupWithNavController(navigationView, navController)
}
override fun onSupportNavigateUp(): Boolean {
return navigateUp(findNavController(this, R.id.nav_host_fragment), drawerLayout)
}