title: Back navigation with Fragments / Toolbar
body: <p>I'm scratching my head with this one now.... I have an <code>ActionBarActivity</code> that loads an initial <code>Fragment</code> - the original menu is inflated within the activity. Now, I have a navigation bar that, when an item is selected, loads a different fragment and adds this to the backstack.</p>
  
  <p>When I do this, there are a couple of things I want to set:</p>
  
  <ol>
  <li>Set the home as up indicator</li>
  <li>Invalidate the options menu from the main activity</li>
  <li>Set has options to true for the Fragment</li>
  <li>Ensure that the up indicator correctly navigates back to the original Fragment</li>
  </ol>
  
  <p>Something rather strange is going on - the up indicator appears once only and does not behave as the back button and although I've invalidated and inflated a new menu, the new menu gets <strong>appended</strong> to the original Activity menu.</p>
  
  <p><strong>EDIT:</strong> Ok I've resolved the appending issue - forgot to add <code>menu.clear()</code> in the <code>onCreateOptionsMenu</code> method.</p>
  
  <p>My navigation drawer layout has <code>onClick</code> methods to all menu items which would trigger the load of another Fragment:</p>
  
  <pre><code>public void navItemClick(View view) {
  
          switch (view.getId()) {
              case R.id.ripSMS:
                  mNavigationDrawer.toggleHome(false);
                  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                  FragmentTransaction mTrans = getSupportFragmentManager().beginTransaction();
                  mTrans.replace(R.id.voiceover_frame_layout,new MessageFragment(),"main_ui")
                          .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).addToBackStack("msg").commit();
                  break;
              case R.id.ripEmail:
                  break;
              case R.id.ripSettings:
                  break;
          }
  
          mNavigationDrawer.closeDrawer();
      }
  </code></pre>
  
  <p>toggleHome:</p>
  
  <pre><code>public void toggleHome(boolean show) {
          mDrawerToggle.setDrawerIndicatorEnabled(show);
  
          if (!show) {
              mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
          } else {
              mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
          }
      }
  </code></pre>
  
  <p>Once the item is triggered the <code>onCreate</code> contains the invalidate and the hasOptions code:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getActivity().invalidateOptionsMenu();
      setHasOptionsMenu(true);
  }
  </code></pre>
  
  <p>The <code>onCreateOptionsMenu</code> then inflates another menu layout (contains a single item called settings).</p>
  
  <p>As mentioned, this only partially works once - the first time I use the item to load the Fragment, I get the back icon but it's also not working (this is set within <code>onOptionsItemSelected</code> to check for the home item press - it does nothing). When I press the back button it takes me back to the correct place. If I navigate back however, the back arrow now longer shows even though the code runs through <code>onCreate</code>!</p>