Search code examples
androidmenugoogle-glassgoogle-gdk

Google Glass voice menu update


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!


Solution

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