Android - settings menu does not work

I am trying to create the "Options" menu in the Office, which is launched from the Service, and then changes its interface based on messages from the Service transmitted through the Handler.

I customize the Options menu as follows:


     /** Menu creation and setup **/

        /* Creates the menu items */
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 1, 0, "Speaker");
            menu.add(0, 2, 0, "Mute");
            return true;
        }

        /* Handles item selections */
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case 1:
                //Do something here
                return true;
            case 2:
               //Do something here
                return true;
            }
            return false;
        }  

But it is never called when my application starts at all.

I am having problems when I need to use a handler to change the text on the screen, since the information is transmitted in the wrong stream, can this same problem be the reason that the menu does not appear?

How can I fix this since I cannot override the method in Handler

+3
source share
2 answers

, . , onCreateOptionsMenu. . :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_MENU)
        return super.onKeyDown(keyCode, event);
    -- insert all other key handling code here --
    return false;
}
+5

super onCreateOptionsMenu :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, 1, 0, "Speaker");
    menu.add(0, 2, 0, "Mute");

    return result;
}
+2

Source: https://habr.com/ru/post/1758174/


All Articles