title: Android - Back button and fragment backstack not working
body: <p>I'm developing a simple fragment-based application with a singe FragmentActivity.  Each 'screen' of the application is contained in a fragment and all fragments are added to the container layout at application startup.  </p>
  
  <pre><code>// Set up fragments in Main Activity
  fragMan = getFragmentManager();
  FragmentTransaction ft = fragMan.beginTransaction();
  ft.add(R.id.fragment_container, settingsFragment);
  ft.add(R.id.fragment_container, mapFragment);
  ft.add(R.id.fragment_container, tracksFragment);
  ft.add(R.id.fragment_container, waypointsFragment);
  ft.commit();
  </code></pre>
  
  <p>Transitions are accomplished by hiding the currently visible fragment, then showing the appropriate fragment.  </p>
  
  <pre><code>ft = fragMan.beginTransaction();
  ft.show(mapFragment);
  ft.addToBackStack(null);
  ft.commit();
  </code></pre>
  
  <p>This all works fine, but when the back button is pressed, the application exits, regardless of which screen is visible or what previous transactions have been added to the back stack.</p>
  
  <p>I've checked to make sure that the back stack is properly accumulating records and tried many different variations of transition methods such as replacing fragments rather than hiding/showing them, creating new instances of fragments rather than storing them in variables, etc.  As far as I can tell, my code matches all the tutorials and examples I can find, and I haven't even been able to find any similar questions/examples of similar problems, presumably because the standard implementation 'just works' for others.</p>
  
  <p>I suspect it may be a problem at the application level such as a property in my manifest (which I've investigated pretty thoroughly) or something inherent to the way my application is set up which prevents the back button from functioning properly.  I can override onBackPressed to handle the transitions manually, but this seems like a very ugly workaround.  Any ideas as to why this might not be behaving as expected? By the way this is on a Nexus 7 running Jelly Bean.</p>