title: Retaining list in list fragment on orientation change
body: <p>I have an app using fragments, all of which are contained in a single activity.  The activity starts with a fragment containing a menu of buttons, all of which cause various listfragments to replace the original button/menu fragment.</p>
  
  <p>My problem is that upon an orientation change, if the activity is displaying one of the listviews, it goes away and the button menu returns.  I understand why this is happening... the activity is destroyed and re-created, but not how to work around it and maintain the list view/current fragment through the orientation change.</p>
  
  <p>I've found <code>setRetainInstance</code> and the example of use <a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.html" rel="noreferrer">here</a>, but I can't figure out how to apply it to my situation with the button menu or the possibility that the fragment I want to retain could be one of several different ones.</p>
  
  <p>Below is code simplified to show the main activity and one of the listfragments.</p>
  
  <p>Any pointers in what to add where to make it so that the list fragment will be retained would be greatly appreciated.</p>
  
  <p><strong>Activity</strong></p>
  
  <pre><code>public class Main extends FragmentActivity {
      private MainMenuFragment menu;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          menu = new MainMenuFragment();
          getSupportFragmentManager().beginTransaction().replace(R.id.pane, menu).commit();    
      }
  }
  </code></pre>
  
  <p><strong>ListFragment</strong></p>
  
  <pre><code>public class ItemListFragment extends ListFragment {
  
      private TextView header;
      private TextView empty;
      private Button add;
      public static Cursor itemCursor;
      private GroceryDB mDbHelper;
      public static long mRowId;
      public static CheckCursorAdapter lists;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.common_list, container, false);
          header = (TextView) v.findViewById(R.id.header);
          empty = (TextView) v.findViewById(android.R.id.empty);
          header.setText(R.string.header_item);
          empty.setText(R.string.empty_items);
          return v;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          mRowId=0;
          mDbHelper = new GroceryDB(getActivity());
          mDbHelper.open();
  
          itemCursor = mDbHelper.fetchAllItems();
          getActivity().startManagingCursor(itemCursor);
  
          String[] from = new String[] { GroceryDB.ITEM_NAME };
          int[] to = new int[] { R.id.ListItem };
          lists = new CheckCursorAdapter(getActivity(),
                  R.layout.listlayout_itemlist, itemCursor, from, to);
          setListAdapter(lists);        
      }
  }
  </code></pre>