Android - Refresh Options Menu without calling invalidateOptionsMenu()
Asked Answered
C

1

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()?

Carlie answered 14/2, 2013 at 3:44 Comment(8)
Is there a reason not to use invalidateOptionsMenu()? That's exactly what it's there for...Sheridan
I've created a classA that extends Activity. Created classB that extends classA. classC (Invokes my XML activity) extends classC.Glycerol
See https://mcmap.net/q/234261/-android-changing-option-menu-items-programmaticallyPorphyritic
Actually, I have created the OptionsMenu, I just have to update the OptionsMenu dynamically.Glycerol
I'm still not seeing why that precludes you using invalidateOptionsMenu(). @Porphyritic has an answer you could use, but I'm still not seeing the problem, I guess.Sheridan
Actually, classC doesn't have the method invalidateOptionsMenu()Glycerol
Plus, calling invalidateOptionsMenu() from superclass of superclass would break the encapsulation. Quoted from SkeetGlycerol
Confirming. In my code the menu content is dependent on certain Model states and some of them are triggered in a deferred way through services work result. The logic of "onResults" could became hard to comprehend really quick and will require a lot of synchroniztion efforts. The onPrepareOptionsMenu solution helps to track all of that in a single place without any complications or fluidity impact as far as your menu changes are limited to hide/show/add/remove. Just KISS.Loge
C
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()

Carlie answered 14/2, 2013 at 10:37 Comment(3)
invalidateOptionsMenu calls onItemSelected of spinner in action bar i have to give a try for this!Grad
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 anymoreEchidna
I cannot see how I could use your solution. Maybe you could help me on a similar problem that I am facing. #54757299Supramolecular

© 2022 - 2024 — McMap. All rights reserved.