是的,正确的方法是,只需添加一个函数调用即可对所需的片段执行相应的导航。
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> {
settings() // function call to navigate to required fragment
true
}
R.id.action_book -> {
book() // function call to navigate to required fragment
true
}
else -> super.onOptionsItemSelected(item)
}
}
fun book(){
// write navigation code here
}
fun settings(){
// write navigation code here
}