(Deprecated) Fragment onOptionsItemSelected not being called

后端 未结 9 2134
孤城傲影
孤城傲影 2020-11-28 20:45

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 o

相关标签:
9条回答
  • 2020-11-28 21:24

    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

    0 讨论(0)
  • 2020-11-28 21:24

    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!

    0 讨论(0)
  • 2020-11-28 21:28

    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

    0 讨论(0)
  • 2020-11-28 21:30

    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)

    0 讨论(0)
  • 2020-11-28 21:34

    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
             ...
    
    0 讨论(0)
  • 2020-11-28 21:39

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

    0 讨论(0)
提交回复
热议问题