Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "onMenuItemSelected" and "onOptionsItemSelected"

Tags:

android

can someone explain me the difference between:

onMenuItemSelected (int featureId, MenuItem item)
http://developer.android.com/reference/android/app/Activity.html#onMenuItemSelected%28int,%20android.view.MenuItem%29

and

onOptionsItemSelected (MenuItem item)
http://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected%28android.view.MenuItem%29

in Android? I found a tutorial were someone overrides both methods.

// Reaction to the menu selection @Override public boolean onMenuItemSelected(int featureId, MenuItem item) {     switch (item.getItemId()) {     case R.id.insert:         createTodo();         return true;     }     return super.onMenuItemSelected(featureId, item); }  @Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {     case R.id.insert:         createTodo();         return true;     }     return super.onOptionsItemSelected(item); } 

Source: http://www.vogella.de/articles/AndroidSQLite/article.html

like image 472
jim Avatar asked Aug 14 '11 20:08

jim


2 Answers

Android knows about several types of menus (e.g. Options Menu and Context Menu). onMenuItemSelected is the generic callback. You don't need to use this usually. onOptionsItemSelected is the callback of the options menu and onContextItemSelected is the callback of the context menu. Use these two specific ones instead.

like image 110
Harald Wilhelm Avatar answered Oct 11 '22 18:10

Harald Wilhelm


Using ADT 17 (version 4.2) the onOptionsItemSelected callback will allow the user to access menu options from any context including the Menu Button and the Action Bar. As of Android version 3.0 the preferred method is the Action Bar which can be accessed from onMenuItemSelected. If you are designing an app that supports versions 2.3 or earlier than onOptionsItemSelected is the way you want to go.

like image 21
rhite1 Avatar answered Oct 11 '22 17:10

rhite1