What does onPrepareOptionsMenu do?
Asked Answered
W

4

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...

Whiffet answered 26/5, 2013 at 10:52 Comment(3)
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. developer.android.com/reference/android/app/…Terzetto
@ZouZou: Sorry, I'm new at Android, I don't know there was a doc until you tell me. Thank you.Whiffet
It is invoked when the user presses the menu button (physical or on the actionbar).Caseinogen
A
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.

Acorn answered 26/5, 2013 at 10:54 Comment(3)
Thank you!! The words "dynamically modify the contents" explain everything!!Whiffet
@Whiffet If this answer satisfies you, you can mark it as correct.Acorn
It would be better if dynamically modify the content could be explained via examplePortauprince
W
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.

Whisler answered 26/5, 2013 at 10:54 Comment(8)
Why would you disable menu option? Could give example the case where we need to disable menu option?Whiffet
I've given an example. If this answer satisfies you, you can mark it as correct, and/or mark it as useful.Whisler
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?Whiffet
I loved to vote up your answer after I have enough reputations.Whiffet
I've only ever used Android 4 and above, so perhaps you should ask another question.Whisler
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?Whiffet
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.Whisler
Oh, I see, I have read this 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.Whiffet
C
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.

Caesarea answered 2/9, 2017 at 18:9 Comment(2)
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.Chip
onCreateOptionsMenu Is called only once. Prepare is called multiple times. That is normal and only be concerned if you experience performance issues.Caesarea
C
0

example

@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if(!URLUtil.isValidUrl(news.geturl())){
        menu.findItem(R.id.share).setVisible(false);
    }
}
Cerumen answered 22/3, 2022 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.