Jenej Fjrjdn
Jenej Fjrjdn

Reputation: 347

onCreateOptionsMenu is never called (toolbar inflate)

My Fragment class:

Toolbar toolbar;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    toolbar =  getView().findViewById(R.id.toolbar3);
    toolbar.inflateMenu(R.menu.menufragmentmain);
    setHasOptionsMenu(true); //i also tried putting this function in oncreate function
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Toast.makeText(getActivity(), "i never enter this function also" , Toast.LENGTH_LONG).show();
    super.onCreateOptionsMenu(menu,inflater);
    inflater.inflate(R.menu.menufragmentmain, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(getActivity(), "i never enter this function" , Toast.LENGTH_LONG).show();
    //some switch cases.....
    return super.onOptionsItemSelected(item);
}

I've got a stupid error which I cant find unfourtantly. I've got a simple toolbar for my fragment. In onViewCreated I inflate my actionbar menu with my toolbar. The issue is that the functions onCreateOptionsMenu and 'onOptionsItemSelected' are never called. I've got no clue why.

Things i checked in other similar questions with the same issue:

None of the points unfourtantly work. What did I miss. Do I need to check something else?

Upvotes: 0

Views: 294

Answers (2)

homerman
homerman

Reputation: 3579

Set up the toolbar in the host Activity. Then override the menu handling in each Fragment wherever necessary.

Here's some generic scaffolding (with comments):

public class MyFragment extends Fragment {
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Report that this Fragment has an options menu
    setHasOptionsMenu(true);
  }

  @Override
  public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    // Inflate then menu, and THEN call super()
    // Note the order of the invocations

    inflater.inflate(R.menu.my_menu, menu);

    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override
  public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    // Handle menu item selections

    final int itemId = item.getItemId();

    switch(itemId) {
      case R.id.my_menu_option_x: ...; return true;
      case R.id.my_menu_option_y: ...; return true;
      case R.id.my_menu_option_z: ...; return true;
      default: return super.onOptionsItemSelected(item);
    }
  }
}

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 200120

As per the Fragment-owned app bar guide which explains how to use a Fragment-owned Toolbar, you do not use any of the setHasOptionsMenu(), onCreateOptionsMenu(), or onOptionsItemSelected() APIs - those are only used for activity owned app bars.

Instead, you would follow the guide for handling menu click events by using the Toolbar APIs:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    toolbar =  getView().findViewById(R.id.toolbar3);
    toolbar.inflateMenu(R.menu.menufragmentmain);

    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(getActivity(), "onMenuItemClick called" , Toast.LENGTH_LONG).show();
            //some switch cases.....
            return true;
        }
    });
}

Upvotes: 2

Related Questions