Fragment's onOptionsItemSelected doesn't get called

后端 未结 5 1946
离开以前
离开以前 2021-02-01 03:17

My fragment replaces the parent Activity options with a specific option item but when I click on the item, only activity\'s onOptionItemSelected gets called eventho

相关标签:
5条回答
  • 2021-02-01 03:40

    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)
  • 2021-02-01 03:45

    I found solution i.e

    Fragment.class

     @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Do something that differs the Activity's menu here
        //MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wifinity_setting, menu);
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
            spanString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, spanString.length(), 0); //fix the color to white
            item.setTitle(spanString);
        }
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
    
            case R.id.menu1:
                Intent intent3 = new Intent(context, activity.class);
                startActivity(intent3);
                return true;
    
        }
        return true;
    }
    

    ACtivity.class

    overide the onOptionsItemSelected() // fragmnets onOptionselected method get called .. this solution works for me

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
    
            default:
                if(fragment != null)
                    fragment.onOptionsItemSelected(item);
        }
        return true;
    }
    
    0 讨论(0)
  • 2021-02-01 03:51

    In my case I did not add any menu items (i.e. I did not call onCreateOptionsMenu in either the activity or the fragment). However, I needed to use the action bar home (up) button in the fragment. For this I had to make sure that setHasOptionsMenu(true) was called in the fragment's onCreateView() method before this could work. Then I didn't need an onOptionsItemSelected override in my activity.

    0 讨论(0)
  • 2021-02-01 03:53

    If your Activity's onOptionsItemSelected method returs true, the call is consumed in activity and Fragment's onOptionsItemSelected is not called. So, return false in your Activity onOptionsItemSelected method or parent class implementation via super.onOptionsItemSelected call (default implementation returns false).

    According Activity class javadoc, method Activity.onOptionsItemSelected should:

    Return false to allow normal menu processing to proceed, true to consume it here

    0 讨论(0)
  • 2021-02-01 03:57

    I agree with the currently accepted solution, but another possible cause is having an ambiguous class reference. I had a custom class in my project named MenuItem and my Fragment was interpreting that custom.MenuItem as the parameter type instead of android.view.MenuItem

    The symptoms were a wiggly red underline on my Override and IDE message indicating that onOptionsItemSelected will not be called.

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