title: Android Toolbar Not Calling onOptionsItemSelected From Fragments On Backstack
body: <p>I recently started updating my app to use the new Toolbar component introduced in Android 5.0 in favor of using a custom view over the action bar. I followed the guide here:
  <a href="http://antonioleiva.com/material-design-everywhere/" rel="noreferrer">http://antonioleiva.com/material-design-everywhere/</a> and adding the Toolbar works fine. The problem is, I am using a navigation structure where I have a MainActivity and replace the content by adding Fragments on to the backstack. I am overriding the onCreateOptionsMenu and onOptionsItemSelected methods in my Fragments to set the menu items in the Toolbar, and the icons change appropriately when I switch Fragments and onOptionsItemSelected is called on the first Fragment, but is not called when I add a Fragment on to the backstack. The onOptionsItemSelected function in the MainActivity is not even called, so the event isn't being consumed by the Activity. I have also tried just replacing the Fragment without adding it to the backstack, but onOptionsItemSelected is still not called. What am I missing to get onOptionsItemSelected to be called once I change the content Fragment? Relevant code is posted below.</p>
  
  <p>App Theme:</p>
  
  <pre><code>&lt;style name="AppThemeLight" parent="@style/Theme.AppCompat.Light"&gt;
      &lt;item name="actionMenuTextColor"&gt;@color/white&lt;/item&gt;
      &lt;item name="android:windowDisablePreview"&gt;true&lt;/item&gt;
      &lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt;
      &lt;item name="android:windowActionBarOverlay"&gt;true&lt;/item&gt;
      &lt;item name="android:windowActionBar"&gt;false&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>Adding Toolbar in MainActivity:</p>
  
  <pre><code>Toolbar toolbar = (Toolbar)findViewById( R.id.toolbar );
  if (toolbar != null) {
      setSupportActionBar( toolbar );
      getSupportActionBar().setDisplayHomeAsUpEnabled( true );
      toolbar.setNavigationIcon( R.drawable.toolbar_icon_menu );
  }
  </code></pre>
  
  <p>Menu functions in MainActivity:</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu( Menu menu ) {
      Log.v( "Main", "onCreateOptionsMenu" );
      return super.onCreateOptionsMenu( menu );
  }
  
  @Override
  public boolean onOptionsItemSelected( MenuItem item ) {
      Log.v( "Main", "onOptionsItemSelected" );
      return super.onOptionsItemSelected( item );
  }
  </code></pre>
  
  <p>Top level Fragment Menu functions:</p>
  
  <pre><code>@Override
  public void onCreateOptionsMenu( Menu menu, MenuInflater inflater ) {
      super.onCreateOptionsMenu( menu, inflater );
      inflater.inflate( R.menu.main_looks, menu );
  }
  
  @Override
  public boolean onOptionsItemSelected( MenuItem item ) {
      switch (item.getItemId()) {
          case R.id.miOptions:
              onOptions();
              return true;
          default:
              return super.onOptionsItemSelected( item );
      }
  }
  </code></pre>
  
  <p>Menu functions in Fragment on backstack</p>
  
  <pre><code>@Override
  public void onCreateOptionsMenu( Menu menu, MenuInflater inflater ) {
      super.onCreateOptionsMenu( menu, inflater );
      inflater.inflate( R.menu.user, menu );
  }
  
  @Override
  public boolean onOptionsItemSelected( MenuItem item ) {
      Log.v( "User", "onOptionsItemSelected" );
      switch (item.getItemId()) {
          case R.id.miUserShare:
              onShareUser();
              return true;
          case R.id.miUserEdit:
              onEditUserProfile();
              return true;
          default:
              return super.onOptionsItemSelected( item );
      }
  }
  </code></pre>