gpt4 book ai didi

android - 无法自定义 Actionbar 的向上导航方法

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:07 25 4
gpt4 key购买 nike

在 Activity 类中,我将操作栏设置为:

我的 Activity

setSupportActionBar(findViewById(R.id.toolbar_my))
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}

因为我需要覆盖onOptionsItemSelected(...)(在 fragment 类中),所以我没有在这里覆盖onSupportNavigateUp()。此 Activity 包含一个 fragment 。我想要的是,当单击操作栏向上按钮时,除了弹回之外,还撤销自定义的 save() 方法。

所以在 fragment 的 onOptionsItemSelected(...) 中,为 item.id == android.R.id.home 写一些代码。不过,我在这里打了一个断点,发现当点击up/home 按钮时,android.R.id.home case 中的代码永远不会被撤销。其他项目对所选方法有效。

在 fragment 类中:

我的 fragment

    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)
}
}
}

我尝试重写Activity类中的另一个onOptionsItemSelected(...)方法,写android.R.id.home case,还是不能调用里面的方法.

为什么没有调用item.id == android.R.id.home情况下的代码?

最佳答案

阅读setHomeButtonEnabled

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)
}

关于android - 无法自定义 Actionbar 的向上导航方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48656394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com