title: How to set the menu of a Toolbar inside a Fragment
body: <p>I have a simple layout with one main <code>Activity</code>, a <code>ViewPager</code> and a <code>BottomNavigationView</code> with 4 buttons. Tapping on each button simply changes the current item on the ViewPager, which loads the correct <code>Fragment</code>.</p>
  
  <p>Each fragment has their own <code>android.support.v7.widget.Toolbar</code>, and my goal is for each one to show different menu items on the Toolbar. I've tried several approaches but haven't been able to make any menu items show up in the Toolbar. This is my code:</p>
  
  <p><strong>Fragment.java</strong></p>
  
  <pre><code>    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_category, container, false);
  
          Toolbar toolbar = (Toolbar) view.findViewById(R.id.main_toolbar);
          toolbar.setTitle("Fragment title");
  
          AppCompatActivity activity = (AppCompatActivity) getActivity();
          activity.setSupportActionBar(toolbar);
          setHasOptionsMenu(true);
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.menu, menu);
          super.onCreateOptionsMenu(menu, inflater);
      }
  </code></pre>
  
  <p><strong>menu.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
      &lt;item
          android:id="@+id/action_add"
          android:enabled="true"
          android:visible="true"
          android:icon="@drawable/ic_add"
          android:title="Add"
          app:showAsAction="always" /&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p>The Activity itself has no menu-related code. Any ideas what I'm missing?</p>