Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is onCreateOptionsMenu(Menu menu)

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);
like image 900
Prashant Avatar asked Jul 05 '16 05:07

Prashant


People also ask

What is onCreateOptionsMenu?

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.

What is the use of Getmenuinflater in Android?

You use it to get a MenuInflater . A MenuInflater can "inflate" menu resources, converting the XML representation into a tree of Menu and MenuItem objects. In turn, those objects are used to populate things like the action bar and Toolbar widgets.

What are menus in Android?

In android, Menu is an important part of the UI component which is used to provide some common functionality around the application. With the help of menu, users can experience a smooth and consistent experience throughout the application.


2 Answers

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.

like image 157
Charles Li Avatar answered Oct 21 '22 15:10

Charles Li


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)
    }
}
like image 26
Felipe Belluco Avatar answered Oct 21 '22 15:10

Felipe Belluco