1

Here is the code in my fragment

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("aaa", "ee");
        switch (item.getItemId()) {
        case R.id.edit:
            Log.e("sssssss","SSSSSSSSSSSSSSs");
            Intent editIntent = new Intent(getActivity(),
                    EditCustomerProfile.class);
            //startActivityForResult(editIntent, EditProfile);
            startActivity(editIntent);
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

I can see the menu item in my action bar.

I inflate the menu like this:

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        int currentTab = mViewPager.getCurrentItem();
        if (currentTab == 1) {
            menu.clear();
            inflater.inflate(R.menu.all_addresses, menu);
        } else if (currentTab == 0) {
            menu.clear();
            inflater.inflate(R.menu.profile, menu);
        }
        return super.onPrepareOptionsMenu(menu);
    }

I am talking about profile menu

but when i click on the edit, nothing happened, even the log is not working

11
  • why the -1 ? i had a problem, i tried to solve it but i couldn't. Feb 26, 2014 at 16:20
  • @blackbelt Fragment from the support v4, it is really weird, i have like millions of fragments on my program, and each one have menu, i don't know why this one doesn't work Feb 26, 2014 at 16:29
  • so onOptionsItemSelected is not called? Do you have onOptionsItemSelected inside the activity?
    – Blackbelt
    Feb 26, 2014 at 16:29
  • @blackbelt yes it is not called, the log is not being printed in the logcat, weird right? Feb 26, 2014 at 16:32
  • 1
    Have you called hasOptionsMenu(true) in the respective fragment's onCreate()? Mar 2, 2014 at 17:42

1 Answer 1

2

It appears that you're creating your menu through your activity, but are trying to intercept menu item selection in your fragment.

In order for the fragment to be included in the call stack of onOptionItemSelected(), you should make ensure that the fragment has called hasOptionsMenu(true) in its respective onCreate().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.