2

I have a ListActivity (SherlockListActivity) and its content can be dynamically changed by the user. When the content changes, the options menu should be replaced.

As I learned, instead of onCreateOptionsMenu I should use onPrepareOptionsMenu that is (supposed to be) called every time the user selects the menu.

This is called right before the menu is shown, every time it is shown.

I have the following code:

@Override
  public boolean onPrepareOptionsMenu(Menu menu)
  {
    menu.clear();
    if (UserOption == UserOption1)
      getSupportMenuInflater().inflate(R.menu.menu_option1, menu);
    else
      getSupportMenuInflater().inflate(R.menu.menu_option2, menu);
    return super.onPrepareOptionsMenu(menu);
  }

It is called only once during debug, so I always have the same menu.

What should I set to make it work?

5
  • stackoverflow.com/a/7066901/3850595 Commented Aug 18, 2015 at 11:44
  • 1
    use invalidateOptionsMenu() in where you want to refresh your menu Commented Aug 18, 2015 at 11:47
  • @JordiCastilla The main problem is that I have to use inflate to create a new layout and a simple reference to a menu does not help. Commented Aug 18, 2015 at 11:47
  • @ArunShankar Almost there, but the menu is replaced only after the first interaction and not immediately. Commented Aug 18, 2015 at 11:51
  • @ArunShankar Using invalidateOptionsMenu is the solution. If you change your answer, I'll accept it. Commented Aug 18, 2015 at 12:08

1 Answer 1

4

Create and prepare the options menu for changing and its item selection method

 @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        MenuInflater inflater = getMenuInflater();
        if(menuString=="red"){
        inflater.inflate(R.menu.red_menu, menu);
        }else if(menuString=="green"){
        inflater.inflate(R.menu.green_menu, menu);
        }
        return true;
    }


 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.new_game:
                return true;
            case R.id.help:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Like whenever you want to change menu call

String menuString="red or green";
invalidateOptionsMenu();

Like others told, if you want to have static menu use onCreateOptionsMenu, and if you want to change its visibility dynamically use onPrepareOptionsMenu along with onCreateOptionsMenu

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

6 Comments

I have to inflate a new layout dinamically. Your solution does not cover that.
what layout, you mean listview..?
A new menu layout, every time.
check my updated answer, it will now clear the existing menu and inflate new one
I also suggest using onPrepareOptionMenu instead of onCreateOptionsMenu.
|

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.