Doubt about the onCreateOptionsMenu method

Asked

Viewed 95 times

0

Could someone explain to me the purpose of this method?

onCreateOptionsMenu

I’ve researched several sites (in the documentation of Android too), but still not clear what the purpose of this method.

1 answer

2


This is one of the public methods available from Activity that can be overridden. Its purpose is to simply create menu options of an Activity inflating an XML (Menu).

It’s the menus that are usually at the top right side of the app.

An example of Whatsapp options menu:

inserir a descrição da imagem aqui

Notes:

  • This method is called only once after the method onStart() in the life cycle of Activity. To make changing menu items dynamic based on events during the Activity lifecycle you can use the method onPrepareOptionsMenu()
  • Menu display may vary in android versions

But why use Android menu Apis?

Although you have the freedom to create something from scratch, using the available Apis is recommended because, in addition to being much easier to create and maintain, the user is already familiar with this type of navigation.


Related:

onOptionsItemSelected()

"Click listener" method from the options menu. With this method you can know which menu item was clicked and perform specific actions.


More about:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.