0

In my project i have activity with options menu. I override onOptionsItemSelected method and add my handlers to menu items (switch-case block). But in one handler i need access to another menu item, how i can do that? findViewById doesn't work

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.visit:
                    //how to access another MenuItem from here?
                    return true;
                }
...
1
  • How could you access a menu item while its not clicked yet? Remeber you are accessing it from onOptionsItemSelected(), which will be called only when the menu item is selected.
    – Chromium
    Commented Jan 4, 2011 at 10:29

1 Answer 1

2

When creating your menu items, you could put the MenuItem you want to check in an attribute (i.e. one of the private fields of your class). This way, when you go into your method, you'll be able toacces the other menu item.

2
  • Thanks, but may be android have native mechanism for access options menu? For example, something like Activity.getOptionsMenu :)
    – Dmitriy
    Commented Jan 4, 2011 at 10:36
  • From what I can grab in the javadoc, doesn't think so. But I don't see what the problem is with using a private field for this, it's basic OOP. Commented Jan 4, 2011 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.