Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateOptionsMenu() calling super

I'm creating application with OptionsMenu. I found few examples with it, but everyone is using different place where to call super.onCreateOptionMenu() in onCreateOptionsMenu() method.

List of different ways:

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

What should I use?

like image 434
skywall Avatar asked Apr 24 '12 18:04

skywall


People also ask

What is onCreateOptionsMenu () in android?

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; }

What is the role of return true or false from the callback method onCreateOptionsMenu?

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

How do you call onCreateOptionsMenu?

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu() .


2 Answers

It depends on what you want to do. First example will place your menu and only your menu. Second one, will add first super class menu. Last one will add your menu first. But, keep in mind that menus also have an order field, which will be taken into account at render time.

Let's say you are extending an activity that already has a menu, but you do not want that menu to appear but another one. In that case you would use first approach.

Another example: you are extending an activity that has a menu, and you want to add another menu. In that case you could use either second or last approach.

like image 143
Cristian Avatar answered Sep 30 '22 19:09

Cristian


The source for onCreateOptionsMenu() is as follows:

public boolean onCreateOptionsMenu(Menu menu) {     if (mParent != null) {         return mParent.onCreateOptionsMenu(menu);     }     return true; } 

Where mParent is the parent Activity of the current Activity. If your Activity extends android.app.Activity then can just return true at the end and not worry about calling the super, as the default implementation will attempt to show a menu based on the parent Activity, which you probably don't want.

like image 22
Jason Robinson Avatar answered Sep 30 '22 18:09

Jason Robinson