无法自定义 Actionbar 的向上导航方法
Cannot custom Actionbar's navigate up method
在 activity class 中,我将操作栏设置为:
MyActivity
setSupportActionBar(findViewById(R.id.toolbar_my))
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
因为需要覆盖onOptionsItemSelected(...)
(片段class),所以这里没有覆盖onSupportNavigateUp()
。
这 activity 包含一个片段。我想要的是,当单击操作栏向上按钮时,除了弹回之外,还撤销自定义的 save() 方法。
所以在片段的 onOptionsItemSelected(...)
中,为 item.id == android.R.id.home
写一些代码。不过,我在这里打了一个断点,发现点击up/home按钮的时候,android.R.id.home
情况下的代码是永远不会撤销的。其他项目的选定方法有效。
片段中class:
MyFragment
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
android.R.id.home -> {
// code here not gets called when click up/home button
mPresenter.save()
return true
}
R.id.edit-> {
// The code here is revoked when item selected.
}
else -> {
return super.onOptionsItemSelected(item)
}
}
}
我尝试重写activityclass中的另一个onOptionsItemSelected(...)
方法,写成android.R.id.home
,仍然无法调用其中的方法。
为什么没有调用item.id == android.R.id.home
案例中的代码?
Enable or disable the "home" button in the corner of the action bar.
(Note that this is the application home/up affordance on the action
bar, not the systemwide home button.)
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
然后
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.getItemId()){
android.R.id.home -> {
mPresenter.save()
return true
}
R.id.edit-> {
// some code
}
}
return super.onOptionsItemSelected(item)
}
在 activity class 中,我将操作栏设置为:
MyActivity
setSupportActionBar(findViewById(R.id.toolbar_my))
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
因为需要覆盖onOptionsItemSelected(...)
(片段class),所以这里没有覆盖onSupportNavigateUp()
。
这 activity 包含一个片段。我想要的是,当单击操作栏向上按钮时,除了弹回之外,还撤销自定义的 save() 方法。
所以在片段的 onOptionsItemSelected(...)
中,为 item.id == android.R.id.home
写一些代码。不过,我在这里打了一个断点,发现点击up/home按钮的时候,android.R.id.home
情况下的代码是永远不会撤销的。其他项目的选定方法有效。
片段中class:
MyFragment
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
android.R.id.home -> {
// code here not gets called when click up/home button
mPresenter.save()
return true
}
R.id.edit-> {
// The code here is revoked when item selected.
}
else -> {
return super.onOptionsItemSelected(item)
}
}
}
我尝试重写activityclass中的另一个onOptionsItemSelected(...)
方法,写成android.R.id.home
,仍然无法调用其中的方法。
为什么没有调用item.id == android.R.id.home
案例中的代码?
Enable or disable the "home" button in the corner of the action bar. (Note that this is the application home/up affordance on the action bar, not the systemwide home button.)
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
然后
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.getItemId()){
android.R.id.home -> {
mPresenter.save()
return true
}
R.id.edit-> {
// some code
}
}
return super.onOptionsItemSelected(item)
}