Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

android - Difference between "onMenuItemSelected" and "onOptionsItemSelected"

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

question from:https://stackoverflow.com/questions/7059572/difference-between-onmenuitemselected-and-onoptionsitemselected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...