title: Resuming a Tic-tac-toe game in Android after closing the fragment
body: <p>I'm learning Android development by creating a test project: Tic-tac-toe. My Tic-tac-toe app starts with a Main Menu activity with a <code>Button</code> that says New Game. After clicking New Game, an activity with a fragment containing a Tic-Tac-Toe game (in a GridLayout) launches. The user can play the game, but when they go back to the Main Menu, the game state is not saved. </p>
  
  <p>I want to change this so that when the user goes back to the Main Menu, they will see a new <code>Button</code> called "Continue" (in addition to the "New Game" Button). Once the user clicks "Continue", the game they were playing before continues. If the click the "New Game" button, a new game will be launched like before.</p>
  
  <p>By using the <code>onSaveInstanceState</code> method and a <code>Bundle</code> <code>savedInstanceState</code>, I was able to preserve the game state on orientation changes (by saving the data from the underlying TicTacToe class). My code below shows how I did that. I would like to do something similar this time again - I have read about it, but I don't quite understand the best way to start. Can anyone show me the right steps?</p>
  
  <p>Note that I'm doing this programmatically (i.e. designing the layout outside the xml files)!</p>
  
  <p>My <code>MainMenu</code> class:</p>
  
  <pre><code>public class MainMenu extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main_menu);
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main_menu, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      public void startGame(View view) {
          Intent intent = new Intent(this,MainGame.class);
          startActivity(intent);
      }
  
  }
  </code></pre>
  
  <p>with a corresponding xml file <code>activity_main_game</code>:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/new_game"
          android:onClick="startGame"
  
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Now my <code>MainGame.class</code> looks like this:</p>
  
  <pre><code>public class MainGame extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main_game);
          if (savedInstanceState == null) {
              getSupportFragmentManager().beginTransaction()
                      .add(R.id.container, new BoardFragment()).commit();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main_game, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      public static class BoardFragment extends Fragment {
  
          public TicTacToe t = new TicTacToe();  //A class I wrote that launches a simple TicTacToe game
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
              if (savedInstanceState != null) {
                  t = new TicTacToe(savedInstanceState.getInt("TicTacToeData"),
                  );
              }
  
      //Graphics stuff here: variable rootView which contains the TicTacToe grid is defined
      //and onClickListeners are added to the ImageViews in the GridLayout which makes corresponding
      //changes to t.
  
              return rootView;
          }
  
          @Override
          public void onSaveInstanceState(Bundle savedInstanceState) {
              savedInstanceState.putInt("TicTacToeData", t.getGameData());
          }
  
      }
  }
  </code></pre>
  
  <p>Thanks for any help.</p>