0

Im my application I inflate different menu resources, like this:

public boolean onCreateOptionsMenu(Menu menu) {
    _menu = menu;
    if (/* CONDITION */) {
        getMenuInflater().inflate(R.menu.menu_activity_detail, _menu);
    } else {
        getMenuInflater().inflate(R.menu.menu_empty, _menu);
    }
    return true;
}

before this I save the Menu object for further reuse, like inflating a new menu under specific circumstances.

Now the question: is there any way to know which resource I've inflated at the creation of my activity, without manually saving the id of R.menu.menu_res?

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
Luca
  • 823
  • 4
  • 11
  • 31
  • What do you need it for? For the menu item clicks? – FD_ Jul 14 '15 at 09:58
  • 1
    In some other method I need to change the menu, but I don't want to clear and inflate the same menu everytime. NowI do `_menu.clear(); getMenuInflater().inflate(R.menu.menu_activity_detail, _menu);`, but most of the times the menu inflated is always the same. I would like to avoid this. – Luca Jul 14 '15 at 10:02
  • 1
    You should not keep a reference to the menu. Why don't you use the invalidateOptionsMenu() method of your Activity? There's the supportInvalidateOptionsMenu() to handle AppCompatActivity – Mimmo Grottoli Jul 14 '15 at 10:17
  • @MimmoGrottoli thanks for the solution! you should post it as an answer :) – Luca Jul 14 '15 at 10:19

1 Answers1

1

You should not keep a reference to the menu. Why don't you use the invalidateOptionsMenu() method of your Activity? There's the supportInvalidateOptionsMenu() to handle AppCompatActivity

Mimmo Grottoli
  • 5,758
  • 2
  • 17
  • 27