Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change menu in onCreateOptionsMenu

I have small problem and I'd like to solve it with dynamical menu:

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    switch(Globals.editMode){
    case Globals.MODE_NONE:
        getSupportMenuInflater().inflate(R.menu.imagehandlingmain_menu, menu);
        break;
    case Globals.MODE_MOVE:
        getSupportMenuInflater().inflate(R.menu.savecancel_menu, menu);
        break;
    case Globals.MODE_ROTATE:
        getSupportMenuInflater().inflate(R.menu.savecancel_menu, menu);
        break;
    case Globals.MODE_SCALE:
        getSupportMenuInflater().inflate(R.menu.savecancel_menu, menu);
        break;
    }
    //getSupportMenuInflater().inflate(R.menu.imagehandlingmain_menu, menu);
    return true;

I have two menus, menu where you can select work mode, and while in certain work mode menu should change to "cancel" and "save".

So the idea is that you enter to one of modes, and then if you decide not to save your work you can cancel it and return to previous state.

Now, as I assumed onCreateOptionsMenu is called only once, so how could / should I "reload" the whole menu when needed?

like image 680
Balkyto Avatar asked Sep 24 '12 08:09

Balkyto


1 Answers

You can use Activity.invalidateOptionsMenu(). This - as the name suggests - will invalidate the current menu and consequently Activity.onPrepareOptionsMenu() will be called again.

like image 197
futtetennista Avatar answered Nov 15 '22 16:11

futtetennista