What is the correct way to create menus in Android?

I am looking through a tutorial and it showed this sample code for creating a menu:

public void onCreateOptionsMenu(Menu m) {
        super.onCreateOptionsMenu(m);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.time_list_menu, m);
    }

I got an error, so I changed it to boolean, this is what it is now, and therefore I did it instead, and its working:

public boolean onCreateOptionsMenu(Menu m) {
      super.onCreateOptionsMenu(m);
      MenuInflater menuInflater = getMenuInflater();
      menuInflater.inflate(R.menu.time_list_menu, m);
      return true;
  }

But I also have such things from another question here, when the stack overflows

Understanding why onCreateOptionsMenu doesn't display a menu

The Android document also has the following:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
  }

Where was the call for super and why is it not needed? What is the correct way to create a menu if I do it wrong?

While I am on this subject, Doc also shows @Override, but I don't have it, and its work. I'm just confused as to whether this is necessary if the methods are explicitly overridden. I appreciate the help. IF any clarification is needed, please let me know.

+3
3

:

  • super.onCreateOptionsMenu: , , , . ( ), , , .
  • @Override: , , . , ( , , ), ( , )

, , .

+3

super, javadoc , . , .

+1

To create android parameters, Menuuse the code below.

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case R.id.yourItemID:
        //do whatever you want here
        break;
    case R.id.yourItemID:
        //do whatever you want here
        break;
    }
    return true;
}

The above code is great for me. Try this tutorial. It will be clearly explained.

0
source

All Articles