title: Android: Saving and Restoring data when switching between Activities
body: <p>I am trying to save some data in "MainActivity" when switching to another activity, and restoring that data as I switch back to it.</p>
  
  <p>In "MainActivity": (restoring data)</p>
  
  <pre><code>            protected void onCreate(Bundle savedInstanceState) {
                     // do usual stuff
                     restoreData();
              }
  </code></pre>
  
  <p>In "MainActivity": (switching to "StatusActivity"):</p>
  
  <pre><code>            Bundle data = saveData();
              Log.d(TAG, "Sending data to status activity intent: " +data);
              Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
              intent.putExtras(data);
              startActivity(intent);  
  </code></pre>
  
  <p>In "StatusActivity":</p>
  
  <pre><code>            Bundle data = getIntent().getExtras();
              Log.d(TAG, "Sending data to main activity intent: " +data);
              Intent intent = new Intent(getApplicationContext(), MainActivity.class);
              intent.putExtras(data);
              startActivity(intent);  
  </code></pre>
  
  <p>My saveData() function:</p>
  
  <pre><code>private Bundle saveData() {
      Log.d(TAG, "Started saving state");
      Bundle data = new Bundle();
  
      // store stuff in the bundle
      return data;
  }
  </code></pre>
  
  <p>My restoreData() function: </p>
  
  <pre><code>private void restoreData() {
      Log.d(TAG, "Started restoring state");
  
      Bundle data = getIntent().getExtras();
              // restore stuff in the bundle
  }
  </code></pre>
  
  <p>LogCat:</p>
  
  <p>My bundle is fine when sending to StatusActivity:</p>
  
  <pre><code>Sending data to status activity intent: 
  Bundle[{obj0=Bundle[{timeSinceLastPooped=3224, hunger=5, 
  id=2130837505, timeSinceLastHungerUpdate=3224, 
  timeSinceLastFed=0, timeSinceLastHappinessUpdate=3224, 
  timeSinceLastEvolution=3224, posY=0.0, posX=0.0, 
  isDead=false, happiness=5, evolutionStage=0, type=pet}], 
  time=7.794168E7}]
  </code></pre>
  
  <p>But then sending back to MainActivity:</p>
  
  <pre><code>Sending data to main activity intent: Bundle[mParcelledData.dataSize=648]
  </code></pre>
  
  <p>What do I do with mParcelledData to get the original bundle back? Thanks!</p>
  
  <h2>Answer:</h2>
  
  <p>In "MainActivity": (restoring data)</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      if(requestCode == 1) {
          if(resultCode == RESULT_OK) {
              restoreData(intent);
          }
      }
  }
  </code></pre>
  
  <p>In "MainActivity": (switching to "StatusActivity"):</p>
  
  <pre><code>            Bundle data = saveData();
              Log.d(TAG, "Sending data to status activity intent: " +data);
              Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
              intent.putExtras(data);
              startActivityForResult(intent, 1);
  </code></pre>
  
  <p>In "StatusActivity":</p>
  
  <pre><code>            Bundle data = getIntent().getExtras();
              Log.d(TAG, "Sending data to main activity intent: " +data);
              Intent intent = new Intent(getApplicationContext(), MainActivity.class);
              intent.putExtras(data);
              setResult(RESULT_OK, getIntent());
              finish();   
  </code></pre>