user1634451 user1634451 - 4 months ago 16
Android Question

(Deprecated) Fragment onOptionsItemSelected not being called

EDIT:
This question was for the deprecated sherlock action bar. Android support library should be used instead now

I have added an action bar menu option called share for my

fragment
which appears but the selection event is not being caught

I am adding it like this

@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
MenuItem item = menu.add(0, 7,0, R.string.share);
item.setIcon(R.drawable.social_share).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}


Trying to capture it in both the
fragment
and the
fragment activity
like

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case 7:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}


and I have
setHasOptionsMenu(true);
in the
onCreate()
.

Answer

Edit for actionbar sherlock use

I had to use

public boolean onMenuItemSelected(int featureId, MenuItem item) {

in the main activity to capture the menu item