0

In my app I'm using onCreatePanelMenu to inflate a menu for the voice recognition. When I inflate a simple menu it works fine, but what I want to do is to inflate a different menu depending on what moment of the program the user is.

Using onCreateOptionsMenu and invalidateOptionsMenu() works fine for gesture menus, put not the same for voice menus.

The code I'm trying to make work is:

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {                    // Voice creation menu...
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
        if(conStateMenu){
            getMenuInflater().inflate(R.menu.voice_aftcon_menu, menu);
        }else{
            getMenuInflater().inflate(R.menu.voice_befcon_menu, menu);
        }
        return true;
    }
    return super.onCreatePanelMenu(featureId, menu);                // Pass through to super to setup touch menu.
}

Where conStateMenu changes at some different point during execution. Again, this works fine with gesture menus, but no with voide ones, is there a way to do this (invalidate and load a new menu)?

Thanks!

gkapellmann
  • 313
  • 3
  • 19
  • `invalidateOptionsMenu` will call `onPrepareOptionsMenu` and not `onCreateOptionsMenu`. – Pedro Oliveira Oct 13 '14 at 14:25
  • Yeah, i saw that also, but if you go to the android [api](http://developer.android.com/reference/android/app/Activity.html) it also says "The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed." at the `invalidateOptionsMenu()` desription, and well, that is actually working fine. – gkapellmann Oct 13 '14 at 14:33
  • And have you tried adding the same login on this function to `onPrepareOptionsMenu`? – Pedro Oliveira Oct 13 '14 at 14:37
  • The thing here is that `onPrepareOptionsMenu(Menu menu)` doesn't have the same arguments as `onCreatePanelMenu(int featureId, Menu menu)`so I cant' reference it to `featureId == WindowUtils.FEATURE_VOICE_COMMANDS`, or am I missing something here? – gkapellmann Oct 13 '14 at 14:42

1 Answers1

0

The method you're looking for is Window#invalidatePanelMenu(int):

getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS);

This will tell the framework that this feature has been invalidated and needs to be re-created/prepared.

Alain
  • 6,044
  • 21
  • 27