使用 ActionBarSherlock 时选项菜单不会动态更新
我需要什么: 创建一个带有“登录”选项的选项菜单,当用户登录时,当用户再次点击菜单按钮时,需要更改为“注销”
使用纯 SDK 时,我只需更改 onPrepareOptionsMenu 中的菜单选项 使用兼容性库 v4 时效果相同 然而,当使用 ActionBarSherlock 时,菜单不会在 onPrepareOptionsMenu 中更新;它仍然会被调用,但显示的菜单不会改变。
有人有解决办法吗?
What I need:
Create an options menu with "Sign in" option, when user signs in it needs to change to "Sign Out" when user taps on the menu button again
When using a pure SDK i can just change the menu options in onPrepareOptionsMenu
the same works when using compatibility library v4
however when using ActionBarSherlock the menu won't update in onPrepareOptionsMenu; it still gets called, but the menu shown does not change.
Does anyone have a solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是 ABS 中的一个令人讨厌的错误。
你可以这样修复它:
在 FragmentActivity.java 中找到 onPrepareOptionsMenu(android.view.Menu menu) 方法并注释掉或删除
行及其相应的右括号,因此每次都会执行该块。
如果您正在使用 FragmentMapActivity 活动,请在其中执行相同的操作。
Yeah, this is a nasty bug in ABS.
You can fix it like this:
In FragmentActivity.java find onPrepareOptionsMenu(android.view.Menu menu) method and comment out or delete
line and it's corresponding closing bracket, so this block gets executed every time.
Do the same in the FragmentMapActivity activity, if you are using it.
该线程有点旧,我无法应用建议的修复(或找到更好的修复)。我正在使用 ActionBarSherlock 4.2,但在选项菜单是覆盖层的 ICS 设备上登录时仍然遇到问题:我无法弄清楚如何仅在登录活动关闭后强制使菜单无效(并且应用程序已确定登录状态已更改)。
我通过标记使菜单
onResume
无效来解决了该问题。我觉得使用标志是一种黑客行为,但这比在每份简历上都失效要好。然后在 onOptionsItemSelected 中我设置了标志:
这对我有用,但我很想知道其他人是否有更好的方法解决它。
This thread is a bit old and I wasn't able to apply the suggested fix (or find a better one). I am using ActionBarSherlock 4.2 but I still have an issue when signing in on ICS devices where the options menu is an overlay: I couldn't figure out how to force invalidating the menu only AFTER the sign-in activity closes (and the app has determined the signed in state changed).
I resolved the issue with a flag to invalidate the menu
onResume
. I feel like using a flag is a hack, but it's better than invalidating on every resume.Then in
onOptionsItemSelected
I set the flag:This is working for me but I'm curious to hear if others have solved it a better way.