(Deprecated) Fragment onOptionsItemSelected not being called

AndroidAndroid FragmentsAndroid Actionbar

Android Problem Overview


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().

Android Solutions


Solution 1 - Android

Same problems happened to me:

> onMenuItemSelected events didn't get called in Fragment

Searched google cann't find a solution, and add onMenuItemSelected method in FragmentActivity doesn't solve it.

Finally resolve it by following reference to http://developer.android.com/guide/topics/ui/actionbar.html

>Note: If you added the menu item from a fragment, via the Fragment class's onCreateOptionsMenu callback, then the system calls the respective onOptionsItemSelected() method for that fragment when the user selects one of the fragment's items. However the activity gets a chance to handle the event first, so the system calls onOptionsItemSelected() on the activity before calling the same callback for the fragment.

Which means only if you don't have that menu item handler in onOptionsItemSelected() on the activity, the onOptionsItemSelected() on the fragment will be called.

Code as following -----Remove the handler for R.action.add on FragmentActivity):

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
        	popBackStack();	        	
            return true;	    
        case R.id.action_search:
            searchAction();
            return true;
        case R.id.action_logout:
        	userLogout();
            return true;
        //case R.id.action_add:
            //return true;    
        default:
            return super.onOptionsItemSelected(item);
    }	
}

And the handler for R.action.add on Fragment looks like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

	Log.d("onOptionsItemSelected","yes");
    switch (item.getItemId()) {
        case R.id.action_add:
        	add();
            return true;    
        default:
            return super.onOptionsItemSelected(item);
    }
}

Finally, remember to add

    setHasOptionsMenu(true);

in your onCreate method in Fragment

Solution 2 - Android

I had the same problem, but I think it's better to summarize and introduce the last step to get it working:

  1. Add setHasOptionsMenu(true) method in your Fragment's onCreate(Bundle savedInstanceState) method.

  2. Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) (if you want to do something different in your Fragment's menu) and onOptionsItemSelected(MenuItem item) methods in your Fragment.

  3. Inside your Activity's onOptionsItemSelected(MenuItem item) method, make sure you return false when the menu item action would be implemented in Fragment's onOptionsItemSelected(MenuItem item) method.

An example:

Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	MenuInflater inflater = getSupportMenuInflater();
	inflater.inflate(R.menu.main, menu);
	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.activity_menu_item:
		// Do Activity menu item stuff here
		return true;
	case R.id.fragment_menu_item:
		// Not implemented here
		return false;
	default:
		break;
	}

	return false;
}

Fragment

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ....
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	// Do something that differs the Activity's menu here
	super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.activity_menu_item:
		// Not implemented here
		return false;
	case R.id.fragment_menu_item:
		// Do Fragment menu item stuff here
		return true;
	default:
		break;
	}

	return false;
}

Solution 3 - Android

I have noticed that the solution people gave you was to implement the code for your menue item in the activity rather then the fragment. I think it will look much more orgenized if you had implemented the code in the fragment rather then the activity 'cos in my opinion it looks better. To do so, do as follows :

Activity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);      
        return true;
    }

 @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {            
        switch (item.getItemId())
        {
            case R.id.SomeIDInTheMenueOfTheActivity:
            {
               //something();
                break;
            }
            default:
             //do something default and add the code under : 
             return super.onOptionsItemSelected(item);
        }
        return true;
    }

Fragment

 @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);  
            setHasOptionsMenu(true);      
        }

  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {           
        super.onCreateOptionsMenu(menu, inflater);
    }
    
     @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch (item.getItemId())
            {           
                case R.id.SomeIDInFragmentMenue:
                {             
                    break;
                }
    
                default:
                    return super.onOptionsItemSelected(item);
            }
    
            return true;
        }

Now the lines (and the likes): "return super.onOptionsItemSelected(item);" in the activity and fragment are super important, because as if you will follow the code in debug, you will see that the menue events functions will be called first on the Activity, and if the item did not match the id's in the activity's switch-case, the degault line : "super.onOptionsItemSelected(item);" will call the onOptionsItemSelected function on the fragment, as we wanted. (if you have many fragments, make sure to have that line in them as well, as the calling hirarchy can be somewhat complicated).

Solution 4 - Android

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

Solution 5 - Android

I'm using actionbarsherlock. This worked for me:

  1. Create dummy_menu.xml menu

    http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="fill_parent" >

  2. In activity inflate the menu like this:

    @Override public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.dummy_menu,menu); return super.onCreateOptionsMenu(menu); }

  3. In fragments onCreateView call setHasOptionsMenu(true) and override onCreateOptionsMenu and onOptionsItemSelected also hide the dummyMenu like this (in fragment)

     @Override
    

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.fragment_actions, menu); MenuItem item = menu.findItem(R.id.dummyMenu); item.setVisible(false); super.onCreateOptionsMenu(menu, inflater); } Hope it helps someone.

Solution 6 - Android

it's so simple you can do that in your fragment to make sure that your action will listen correctly:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Solution 7 - Android

I had this problem. It was because I was overiding the wrong method

onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) is what I used.

Make sure you are using the right one!

Solution 8 - Android

You are not chaining to the superclass in the activity methods. Please have onCreateOptionsMenu() return super.onCreateOptionsMenu(menu), and have onOptionsItemSelected() return super.onOptionsItemSelected(item) (except for the item that you are handling, which should return true to indicate that you have handled the event)

Solution 9 - Android

you must add this code toolbar.bringToFront(); next set toolbar in your activity

 public class MainActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
        ...

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("Yazd");
        setSupportActionBar(toolbar);
        toolbar.bringToFront(); // <<= add here
         ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1634451View Question on Stackoverflow
Solution 1 - AndroidFelixqkView Answer on Stackoverflow
Solution 2 - AndroidMarco HernaizView Answer on Stackoverflow
Solution 3 - AndroidMalfondeView Answer on Stackoverflow
Solution 4 - Androiduser1634451View Answer on Stackoverflow
Solution 5 - AndroidokkkoView Answer on Stackoverflow
Solution 6 - AndroidMohamed HussienView Answer on Stackoverflow
Solution 7 - AndroidBarrie GalitzkyView Answer on Stackoverflow
Solution 8 - AndroidDivyesh MuraniView Answer on Stackoverflow
Solution 9 - AndroidMahdi AfkhamiView Answer on Stackoverflow