title: Changing ActionBar when using fragments
body: <p>In a Fragment, called within an activity I'm displaying a list of bus lines like this:</p>
  
  <p><img src="https://i.stack.imgur.com/HcPOh.png" alt="Page 1, when opening for the first time."></p>
  
  <p>Then, once the user clicks on "Stations", I like to show a list of stations of course.
  I'm using this code:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.act_long_distance);
  
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.add(R.id.f_long_distance, new LongDistanceFragment()).commit();
  }
  
  @SuppressWarnings({"UnusedDeclaration"})
  public void showStationList(View view) {
      String tag = (String) view.getTag();
      if (tag != null &amp;&amp; tag.length() &gt; 0) {
          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          StationListFragment fragment = new StationListFragment(tag.split(","));
          ft.add(R.id.f_long_distance, fragment);
          // ft.replace(R.id.f_long_distance, fragment);
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
          ft.addToBackStack(null);
          ft.commit();
      }
  }
  </code></pre>
  
  <p>The XML for this activity is:</p>
  
  <pre><code>&lt;LinearLayout
      android:id="@+id/ll_container"
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      &gt;
      &lt;FrameLayout
          android:id="@+id/f_long_distance"
          android:layout_weight="1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>StationListFragment is a simple <strong>ListFragment</strong> displayed on top of the other:</p>
  
  <p><img src="https://i.stack.imgur.com/jDgGc.png" alt="After clicking on the Station List button"></p>
  
  <p>What works well though is the ActionBar, it now properly contains the Title only.</p>
  
  <p><strong>What doesn't work is if I press <em>back</em> now.</strong> The Station List is hidden, but the old ActionBar is not restored:</p>
  
  <p><img src="https://i.stack.imgur.com/gRkKy.png" alt="ActionBar after coming back from List button"></p>
  
  <p>The docs are saying that the way to add the ActionBar is using onCreateOptionsMenu methods etc. </p>
  
  <p>So, in LongDistanceFragment (the first one shown), I'm creating the bar like this:</p>
  
  <pre><code>@Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      Log.d(TAG, "onViewCreated");
      super.onViewCreated(view, savedInstanceState);
  
      setHasOptionsMenu(true);
  }
  
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      super.onCreateOptionsMenu(menu, inflater);
  
      ActionBar bar = getSupportActivity().getSupportActionBar();
      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
      bar.setListNavigationCallbacks(new SimpleSpinnerArrayAdapter(getActivity()), this);
  }
  </code></pre>
  
  <p>But somehow it is not restored once the user is back in that Fragment.</p>
  
  <p>I think a way to recover the state of the ActionBar when rolling back the Fragment Transaction is needed.</p>
  
  <p>What am I missing? Thanks for any help.</p>