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.

The generated code is wrong when extending activities annotated with @OptionsMenu  #473

Closed

Description

@mathieuboniface

Reported by Charlie Babitt on the mailing list :
https://groups.google.com/forum/?fromgroups=#!topic/androidannotations/3wUAMibQa-8

I have the following problem. I have two AA annotated activities. Activity A has an @optionsmenu extension. I want activity B to extend from A but have another @optionsmenu. Doing this I end up with a compilation error in my generated code, since it generates two public boolean onCreateOptionsMenu(Menu menu) which leads to a duplicate method error...

Is there any idea how i can fix it other than removing the annotation from activity A and put it to all its child classes except B?

Activity

  1. mathieuboniface commented on Jan 26, 2013

    @mathieuboniface
    ContributorAuthor

    I've just provided a fix on that issue.

    Now, when multiple @OptionsMenu are processed from the class hierarchy, only one method is created, and this method inflate all the declared menu from super classes annotations.

    Example :

    @EActivity
    @OptionsMenu({R.menu.my_menu, R.menu.my_menu2})
    public class OptionsMenuActivity extends Activity {
    
        // ...
    
    }
    @EActivity
    @OptionsMenu(R.menu.my_menu3)
    public class OptionsMenuSubActivity extends OptionsMenuActivity {
    
        // ...
    
    }

    Will produce the following code :

    public final class OptionsMenuSubActivity_ extends OptionsMenuSubActivity {
    
        // ...
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(org.androidannotations.test15.R.menu.my_menu, menu);
            menuInflater.inflate(org.androidannotations.test15.R.menu.my_menu2, menu);
            menuInflater.inflate(org.androidannotations.test15.R.menu.my_menu3, menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        // ...
    
    }

    @charlie
    To resolve your issue, you can try the latest snapshot from here : https://oss.sonatype.org/content/repositories/snapshots/org/androidannotations/androidannotations-bundle/3.0-SNAPSHOT/

    We could enhance AA flexibility by providing a way to override @OptionsMenu declared from super classes. Any thoughts appreciated !
    Example :

    @EActivity
    @OptionsMenu({R.menu.my_menu, R.menu.my_menu2})
    public class OptionsMenuActivity extends Activity {
    
        // ...
    
    }
    @EActivity
    @OptionsMenu(value=R.menu.my_menu3, override=true)
    public class OptionsMenuSubActivity extends OptionsMenuActivity {
    
        // ...
    
    }

    Should produce the following code :

    public final class OptionsMenuSubActivity_ extends OptionsMenuSubActivity {
    
        // ...
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(org.androidannotations.test15.R.menu.my_menu3, menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        // ...
    
    }
  2. odbol commented on Feb 5, 2013

    @odbol

    I am having the same issue, although I can't even inherit my Activity because it's final. On this page, it says "we favor composition over inheritance" and I'd love to know if there's a way to just inject the OptionsMenu functionality into all my Activities.

    I think keeping OptionMenu stuff in a separate EBean class would be cool, because then you could just inject several of them into your Activity and it would combine them for you. This would also make it easy to add a single menu item to the global application menu only on certain activities (for instance, a Share button). Do you think this is possible?

  3. odbol commented on Feb 6, 2013

    @odbol

    To expand on your example:

    @EBean
    @OptionsMenu({R.menu.my_menu})
    public class MyOptionsMenu {
    
        // ...
    
    }
    

    And then you would inject it into your Activity like this:

    @EActivity
    public class OptionsMenuActivity extends Activity {
    
       @EBean
       MyOptionsMenu globalOptions;
    
    
       @EBean
       MyShareOptionsMenu shareOptions;
        // ...
    
    }
    
  4. mathieuboniface commented on Feb 23, 2013

    @mathieuboniface
    ContributorAuthor

    Hi @odbol,

    Sorry for the late answer, i'm currently switching to a new job...

    Actually, there is now way to define application-wide options menu.

    The main issue I see with your proposal is how to deal with incremental compilation. In fact AA does not keep any state between each compilation, so when OptionsMenuActivity is compiled, nothing is known about MyOptionsMenu and the right code cannot be generated.

  5. DayS commented on Jan 20, 2014

    @DayS
    Contributor

    All of this should (but the override option) be fixed now.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions