18

Hello Android Developers,

I have seen a lot of question regarding the update of Options Menu. However, every answer says I have to call invalidateOptionsMenu().

My question is, is there any other way of updating the Options Menu without invoking the method invalidateOptionsMenu()?

8
  • 7
    Is there a reason not to use invalidateOptionsMenu()? That's exactly what it's there for... Commented Feb 14, 2013 at 3:46
  • I've created a classA that extends Activity. Created classB that extends classA. classC (Invokes my XML activity) extends classC. Commented Feb 14, 2013 at 3:50
  • See stackoverflow.com/a/11699930/597657 Commented Feb 14, 2013 at 3:53
  • Actually, I have created the OptionsMenu, I just have to update the OptionsMenu dynamically. Commented Feb 14, 2013 at 4:00
  • 1
    I'm still not seeing why that precludes you using invalidateOptionsMenu(). @Eng.Fouad has an answer you could use, but I'm still not seeing the problem, I guess. Commented Feb 14, 2013 at 4:25

1 Answer 1

22
@Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        menu.clear(); // Clear the menu first

            /* Add the menu items */

        return super.onPrepareOptionsMenu(menu);
    }

This solved the problem on updating the OptionsMenu without invoking the invalidateOptionsMenu()

Sign up to request clarification or add additional context in comments.

3 Comments

invalidateOptionsMenu calls onItemSelected of spinner in action bar i have to give a try for this!
this works! I can't use invalidateOptionsMenu because somehow after calling that, the toast that suppose to show up after long pressing menu item icon does not work anymore
I cannot see how I could use your solution. Maybe you could help me on a similar problem that I am facing. stackoverflow.com/questions/54756799/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.