36

I want to make Option Menu for Android, I have visit this site. In their script, I found onPrepareOptionsMenu, I try to compile and run using Android 2.3.3 compiler with and without onPrepareOptionsMenu, both works, but I didn't see any difference.

public boolean onCreateOptionsMenu(Menu menu){
    //code here
}
    
public boolean onOptionsItemSelected(MenuItem item){
    //code here
}
    
public boolean onPrepareOptionsMenu(Menu menu){
    //code here
}

What is actually onPrepareOptionsMenu method do? Is that method important? Could I just delete the method?


Addition

Oh, I also hear about Action Bar in Android 3.0, it says that Action Bar is the alternative way for make Option Menu, and it using onPrepareOptionsMenu. Is that right?

Thank you...

stkent
  • 19,772
  • 14
  • 85
  • 111
Tutompita
  • 639
  • 1
  • 5
  • 14
  • Just read doc please. It has been created for that. If you don't understand a specific point of what it does, ask it then. http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu) – Alexis C. May 26 '13 at 10:56
  • @ZouZou: Sorry, I'm new at Android, I don't know there was a doc until you tell me. Thank you. – Tutompita May 26 '13 at 11:13
  • 1
    It is invoked when the user presses the menu button (physical or on the actionbar). – IgorGanapolsky Dec 02 '13 at 18:36

4 Answers4

50

Take a look in the API:

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

Tom Naessens
  • 1,827
  • 22
  • 38
8

If you want to alter the menu before it's shown to the user, you can put code to do that into onPrepareOptionsMenu. I've used that dynamically to disable some menu options in some circumstances.

As an example of when one might want to disable a menu option, I had an app where there was a way of specifying a destination. One of my menu options was to calculate a route to the destination. However, if a destination wasn't specified, that option didn't apply, so I used onPrepareOptionsMenu to disable that menu option when it wasn't applicable.

From Android 3.0 and beyond, there's the ActionBar, which is a menu bar. The most important items go into the ActionBar itself, but then there's an overflow for when there's not enough room on the action bar. One can specify that menu items should always be in the overflow menu and never on the action bar itself. On some devices, the action bar overflow corresponds to the permanent menu button on the device, whereas on other devices which don't have a menu button the overflow menu is seen on the right hand side of the action bar as three vertical dots.

Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • Why would you disable menu option? Could give example the case where we need to disable menu option? – Tutompita May 26 '13 at 11:33
  • 1
    I've given an example. If this answer satisfies you, you can mark it as correct, and/or mark it as useful. – Stochastically May 26 '13 at 11:54
  • Thanks for the example you've given, it give me another idea of this methode. I have seen an apps with the three vertical dots, but it runs in my 2.3.4 Android. How they make it? – Tutompita May 26 '13 at 18:15
  • I loved to vote up your answer after I have enough reputations. – Tutompita May 26 '13 at 18:17
  • 1
    I've only ever used Android 4 and above, so perhaps you should ask another question. – Stochastically May 26 '13 at 18:42
  • Oh yes I have another question, are you using onOptionsItemSelected for Option Menu (Option Menu is different from Action Bar right?) or using ActionBar, for the hardware Menu Button in Android 4.0? – Tutompita May 27 '13 at 19:16
  • 1
    My minimum Android version is 4.0, so I'm only working with the ActionBar. I've never worked with any other kind of option menu in Android. Note that you can't control whether you get the three vertical dots for the overflow menu, or whether the overflow menu goes to the hardware menu button. If there is a hardware menu button, then all versions of Android that I'm familiar with will automatically use it for the ActionBar overflow. – Stochastically May 27 '13 at 19:19
  • Oh, I see, I have read [this](http://stackoverflow.com/questions/15250024/show-options-menu-button-on-galaxy-nexus-android-4-1) too. So, from what I read the ActionBar only works for API 11 and above, OptionMenu only works for API 10 and below. To make ActionBar for API 10 and below (like the apps with three-dots menu I see), it says we can use ActionBarSherlock, and to make OptionMenu for API 11 above, we should make the build target up to API 13. – Tutompita May 27 '13 at 21:03
4

onCreateOptionsMenu is called once, when your activity is first created. If it returns false, no option menu is shown and onPrepareOptionsMenu is never called.

If onCreateOptionsMenu returns true, onPrepareOptionsMenu is also called before the activity is displayed, and also every time the options menu is invalidated. Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.

If your menu does not change, use onCreateOptionsMenu.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • Hi, thank you for this answer, I have a question tho, Every time i click a menu item onPrepareOptionsMenu is called, (i've put a log there), shouldn't it be called once only when the activity is displayed? The app is working fine i'm just worried about this. – Doilio Matsinhe Jul 05 '19 at 13:40
  • 1
    onCreateOptionsMenu Is called only once. Prepare is called multiple times. That is normal and only be concerned if you experience performance issues. – Steven Spungin Jul 05 '19 at 16:12
0

example

@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if(!URLUtil.isValidUrl(news.geturl())){
        menu.findItem(R.id.share).setVisible(false);
    }
}
Hesham Fas
  • 876
  • 1
  • 9
  • 20