20

What are the two parameters Menu and menu in method onCreateOptionsMenu(Menu menu) and how to use this method. I have another question why this parameter is used in

Intent intent = new Intent(this, DisplayMessageActivity.class);
zapping
  • 4,118
  • 6
  • 38
  • 56
Prashant
  • 381
  • 1
  • 2
  • 7
  • Menu is Class & menu is object of that class. – Suhas Bachewar Jul 05 '16 at 05:09
  • 1
    Intent will take the `Context` as the first parameter. In the statement `Intent intent = new Intent(this, DisplayMessageActivity.class);`, `this` parameter represents current context of the class. – SripadRaj Jul 05 '16 at 05:12

3 Answers3

37

Menu is just the type of the parameter menu. For example you can have a String type for a variable named string, dog, etc. And in this case there's a Menu type for a parameter named menu.

You use onCreateOptionsMenu() to specify the options menu for an activity. In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback.

For example:

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

Fore more information, visit this link.

As for this,

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.

For example:

public void sendMessage() {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
}

The constructor takes two parameters and a Context as its first parameter. this represents environment data and provides global information about an application environment.

For more information on the intent example you provided, check this out.

Maddy
  • 4,525
  • 4
  • 33
  • 53
Charles Li
  • 1,835
  • 3
  • 24
  • 40
  • I would also like to include this [link] (https://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)), I find the link in the answer tells you how setup onCreateOptionsMenu but doesn't do a good job describing the method. – Shawn Jul 20 '19 at 00:44
  • And don't forget setHasOptionsMenu(true) on `onCreate` – Alexmelyon May 16 '22 at 13:06
27

The intent of implementing this method is to populate the menu passed with the items you define in the R.menu.game_menu layout file.

#Java

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

#Kotlin

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.game_menu, menu)
    return true
}

After inflating the menu with the items you might want to add some action when they are selected:

Java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_item:
            // Action goes here
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Kotlin

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        R.id.menu_item -> {
            // Action goes here
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
}
y_159
  • 458
  • 3
  • 15
Felipe Belluco
  • 1,102
  • 10
  • 12
0

When onCreateOptionsMenu is called?

onCreateOptionsMenu() is called by the Android runtime when it need to create the option menu.

Android Developer Guide: Menus

If you've developed your application for Android 2.3.x and lower, the system calls onCreateOptionsMenu() to create the options menu when the user opens the menu for the first time. If you've developed for Android 3.0 and higher, the system calls onCreateOptionsMenu() when starting the activity, in order to show items to the app bar.

How to build an option menu?

Please refer to other anwsers.

Why onCreateOptionsMenu return Boolean

Activity.html#onCreateOptionsMenu

You must return true for the menu to be displayed; if you return false it will not be shown.

Why onOptionsItemSelected return Boolean

When you successfully handle a menu item, return true. If you don't handle the menu item, you should call the superclass implementation of onOptionsItemSelected() (the default implementation returns false).

aztack
  • 4,376
  • 5
  • 32
  • 50