You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
@OptionsItem doesn't allow subclasses to override menu item handling #1067
The generated code for onOptionsItemSelected calls your superclass's onOptionsItemSelected() before the subclass, removing your ability to override functionality in the subclass. I believe the only workaround is to override onOptionsItemSelected() in your activity (rather than use @OptionsItem annotation) because I don't think there's a way to achieve the same goal using AndroidAnnotations.
Here's the generated code. My base activity class handles android.R.id.home.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = super.onOptionsItemSelected(item);
if (handled) {
return true;
}
int itemId_ = item.getItemId();
if (itemId_ == android.R.id.home) {
onHomeTapped();
return true;
}
return false;
}
I think our current generated code is OK. All menu items should have a different ID. If an item is clicked with the ID, the action is handled, so it should return true. If the superclass cannot handle this ID, then it is delegated to the subclass which may added more handler methods. If an action corresponding to a specific ID should be different with the subclass, it can simply override the @OptionsItem annotated method. Your problem here is you want to handle the android.R.id.home ID in two different places at once: in your superclass and the child, too (if i get it good).
Please tell me if you think my reasoning is not OK.
I only want to handle it in my subclass - Essentially, I want to override the base activity's onOptionsItemSelected. The current generated code does not allow you to do it, and to be honest it really doesn't make any sense, and it goes against all principles of inheritance.
I don't think I've done a good job of communicating the problem, and I'm sorry, but I'm happy to make a sample project if that would help.
It's not an issue if the base class is an abstract class with @EActivity and @OptionsItem annotated method, but if you override onMenuItemSelected in the base activity then the issue that I described occurs.
Activity
WonderCsabo commentedon Jul 7, 2014
I cannot really understand. You can simply override
onHomeTapped()and the overriden method will be called due to dynamic binding. What do you mean?jacobtabak commentedon Jul 7, 2014
The method returns right away because handled = true, the second half of the method is never reached, so the generated code is useless.
jacobtabak commentedon Jul 7, 2014
I would think the generated code should look like this:
WonderCsabo commentedon Jul 7, 2014
What is in
super.onOptionsItemSelected()in this case?jacobtabak commentedon Jul 7, 2014
Please ignore whether or not it makes sense to have code like this in the superclass, just inherited this codebase.
jacobtabak commentedon Jul 7, 2014
I'd be happy to make a sample project or something that demonstrates this issue, if that would help.
WonderCsabo commentedon Jul 7, 2014
I think our current generated code is OK. All menu items should have a different ID. If an item is clicked with the ID, the action is handled, so it should return
true. If the superclass cannot handle this ID, then it is delegated to the subclass which may added more handler methods. If an action corresponding to a specific ID should be different with the subclass, it can simply override the@OptionsItemannotated method. Your problem here is you want to handle theandroid.R.id.homeID in two different places at once: in your superclass and the child, too (if i get it good).Please tell me if you think my reasoning is not OK.
jacobtabak commentedon Jul 7, 2014
I only want to handle it in my subclass - Essentially, I want to override the base activity's onOptionsItemSelected. The current generated code does not allow you to do it, and to be honest it really doesn't make any sense, and it goes against all principles of inheritance.
I don't think I've done a good job of communicating the problem, and I'm sorry, but I'm happy to make a sample project if that would help.
WonderCsabo commentedon Jul 7, 2014
OK, i'll check the project out.
BTW, your suggestion is making sense in this comment, i agree.
jacobtabak commentedon Jul 7, 2014
https://drive.google.com/file/d/0BzVkvWjM0kCTN2VtZkpuUGx3VkU/edit?usp=sharing
It's not an issue if the base class is an abstract class with @EActivity and @OptionsItem annotated method, but if you override onMenuItemSelected in the base activity then the issue that I described occurs.
WonderCsabo commentedon Aug 16, 2014
You are correct. Can you create a PR with the change you suggested?
WonderCsabo commentedon Dec 28, 2014
@yDelouis do you agree with this?
yDelouis commentedon Dec 28, 2014
I agree with @jacobtabak. The call to
super.onOptionsItemSelected(item)should be the last thing in the generated method.WonderCsabo commentedon Mar 27, 2015
Implemented.