Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
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

Description

@jacobtabak

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

Activity

  1. WonderCsabo commented on Jul 7, 2014

    @WonderCsabo
    Member

    I cannot really understand. You can simply override onHomeTapped() and the overriden method will be called due to dynamic binding. What do you mean?

  2. jacobtabak commented on Jul 7, 2014

    @jacobtabak
    Author

    The method returns right away because handled = true, the second half of the method is never reached, so the generated code is useless.

  3. jacobtabak commented on Jul 7, 2014

    @jacobtabak
    Author

    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);
        }
    
  4. WonderCsabo commented on Jul 7, 2014

    @WonderCsabo
    Member

    What is in super.onOptionsItemSelected() in this case?

  5. jacobtabak commented on Jul 7, 2014

    @jacobtabak
    Author

    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;  
        }
    
  6. jacobtabak commented on Jul 7, 2014

    @jacobtabak
    Author

    I'd be happy to make a sample project or something that demonstrates this issue, if that would help.

  7. WonderCsabo commented on Jul 7, 2014

    @WonderCsabo
    Member

    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.

  8. jacobtabak commented on Jul 7, 2014

    @jacobtabak
    Author

    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.

  9. WonderCsabo commented on Jul 7, 2014

    @WonderCsabo
    Member

    OK, i'll check the project out.

    BTW, your suggestion is making sense in this comment, i agree.

  10. jacobtabak commented on Jul 7, 2014

    @jacobtabak
    Author

    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.

  11. WonderCsabo commented on Aug 16, 2014

    @WonderCsabo
    Member

    You are correct. Can you create a PR with the change you suggested?

  12. WonderCsabo commented on Dec 28, 2014

    @WonderCsabo
    Member

    @yDelouis do you agree with this?

  13. yDelouis commented on Dec 28, 2014

    @yDelouis
    Contributor

    I agree with @jacobtabak. The call to super.onOptionsItemSelected(item) should be the last thing in the generated method.

  14. self-assigned this
    on Dec 28, 2014
  15. WonderCsabo commented on Mar 27, 2015

    @WonderCsabo
    Member

    Implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions