-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add a @OptionMenuItem annotation to inject MenuItem #567
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if extended SherlockActivity contains android MenuItem or vice versa? I haven't tried, but it seems that the user will get an error in generated folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you're right. We need to add a check on the super class.
|
👍 Great feature! |
…ty and vice-versa
…SherlockActivity and vice-versa
Add a @OptionMenuItem annotation to inject MenuItem
|
Perhaps the wiki should state that when injecting an OptionsMenuItem you cannot use the injected variable in methods annotated with @AfterInject. The life cycle of Android is onCreate() --> OnStart() --> OnResume --> OnCreateOptionsMenu(), whereas @AfterInject runs in onCreate() and @OptionsMenuItem runs in OnCreateOptionsMenu(). Unless im completely lost here? |
|
Nop, you're right :) More precisely, However, menu items still not useable in |
|
So just to get this right, if I need to disable a button when I first initialize it (an action that I would usually do inside onCreateOptionsMenu) where can I do that if I want to use the annotation? |
|
Funny when you are searching the answer of something, only to realize that you are the one that asked the same question 11 months ago ! I am guessing that one cannot do that with androidannotations. |
|
Thanks for the explanation guys, I had some trouble with it. |
|
So there is still no solution for this question ? Which means we can not use annotation with menu options ? |
|
@TomIsYourName what is your problem exactly? |
|
If I'm guessing your question correctly, @TomIsYourName, I would say "no, you can't". Not the way I think you mean at least. Your menu items aren't available in I would suggest you calculate your desired menu item state in either The below code shows an example where you fetch a couple of items from the network and based on whether that result is empty or not, you set your menu item in question to either enabled or disabled: A different, maybe more down-to-earth example that would describe the same situation could be if you want to give your grand child a T-shirt. You can't dress him in it now, because he's not born yet. You can still buy the T-shirt now and put it in your wardrobe, but you have to await his birth before you can give it to him. public class MyActivity extends AppCompatActivity {
@Bean
protected MyNetworkClient myNetworkClient;
private MenuItem myMenuItem = null;
private boolean shouldBeEnabled = false;
@AfterInject
protected void afterInject() {
myNetworkClient.getProductNames(new SuccessListener() {
public void onSuccess(List<String> productNames) {
shouldBeEnabled = !productNames.isEmpty();
updateMyMenuItem();
}
});
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
myMenuItem = menu.findItem(R.id.action_continue);
updateMyMenuItem();
}
private void updateMyMenuItem() {
if (myMenuItem != null) {
myMenuItem.setEnabled(shouldBeEnabled);
}
}
}If you strictly want to, you could maybe even replace the @InjectMenu
protected void injectMenu(Menu menu) {
myMenuItem = menu.findItem(R.id.action_continue);
updateMyMenuItem();
} |
|
you can use @OptionsMenuItem(R.id.action_continue)
void singleInjection(MenuItem item) {
// do stuff with `item`
}this method will be called once your menu has been inflated. |
It worked for me. Thank you. @dodgex |
Referred to #560 and #549
This PR add a
@OptionMenuItemwhich allows the developer to inject a MenuItem (android or ActionBarSherlock) into an activity.The developer should just be aware that injection is done in
onCreateOptionsMenu()so the fields will be set only when the menu is displayed (immediately if an ActionBar is provided, otherwise when the user press the Menu button)