Repository navigation
@OptionsItem doesn't allow subclasses to override menu item handling #1067
Description
Activity
I cannot really understand. You can simply override
onHomeTapped()and the overriden method will be called due to dynamic binding. What do you mean?The method returns right away because handled = true, the second half of the method is never reached, so the generated code is useless.
I would think the generated code should look like this:
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { onHomeTapped(); return true; } return super.onOptionsItemSelected(item); }What is in
super.onOptionsItemSelected()in this case?Please ignore whether or not it makes sense to have code like this in the superclass, just inherited this codebase.
@Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); if (itemId == android.R.id.home) { startActivity(MainActivity.getLaunchIntent(this)); finish(); return true; } return false; }I'd be happy to make a sample project or something that demonstrates this issue, if that would help.
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.
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.
OK, i'll check the project out.
BTW, your suggestion is making sense in this comment, i agree.
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.
You are correct. Can you create a PR with the change you suggested?
@yDelouis do you agree with this?
I agree with @jacobtabak. The call to
super.onOptionsItemSelected(item)should be the last thing in the generated method.Implemented.
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.