user1634451 user1634451 - 2 years ago 237
Android Question

Fragment onOptionsItemSelected not being called

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

public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)
MenuItem item = menu.add(0, 7,0, R.string.share);

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

public boolean onOptionsItemSelected(MenuItem item) {

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

and i have setHasOptionsMenu(true); in the onCreate()

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download