title: Restoring state not working when returning to activity, savedInstanceState is null
body: <p>So im having some trouble restoring the state of my <code>Activity</code>.
  At this point I figure that its probably a problem with my understanding rather then anything else.</p>
  
  <p>My goal is to move from my <code>MainActivity</code> to a <code>Main2Activity</code>.
  Am I correct in thinking that when a user moves from one page to another, it should be done by changing <code>Activity</code> via <code>Intent</code>?
  I am doing this like so:</p>
  
  <p>The <code>onCreate()</code> for my <code>MainActivity</code> has this in it.</p>
  
  <pre><code>Button currentButton = (Button) findViewById(R.id.button2);
  currentButton.setOnClickListener(
      new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              setContentView(R.layout.activity_main2);
              Intent nextIntent = new Intent(getApplicationContext(), Main2Activity.class);
              startActivity(nextIntent);
          }
      }
  );
  </code></pre>
  
  <p>Which as I understand should call <code>onCreate()</code>, <code>onStart()</code> and <code>onResume()</code> for Main2Activity, then <code>onSaveInstanceState()</code> for <code>MainActivity</code>, then <code>onStop()</code> for <code>MainActivity</code>.
  Ive overloaded all those functions with logging and seen that indeed they are being called and in that order.</p>
  
  <p>Here is my <code>onSaveInstanceState()</code> for <code>MainActivity</code>:</p>
  
  <pre><code>@Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
      System.out.println("onSaveInstanceState called, saving state");
      savedInstanceState.putInt("mySuperUniqueKey", testInt);
  
      super.onSaveInstanceState(savedInstanceState);
  }
  </code></pre>
  
  <p>Once in <code>Main2Activity</code>, I return back to <code>MainActivity</code> in the same way. I.e. findViewById() the button, overload its <code>onClickListener()</code>, create a new <code>Intent</code> and start it.</p>
  
  <p>Then <code>MainActivity</code> class's <code>onCreate()</code> has this :</p>
  
  <pre><code>if (savedInstanceState != null) {
      System.out.println("savedInstanceState is not null");
      testInt = savedInstanceState.getInt("mySuperUniqueKey");
  } else {
      System.out.println("savedInstanceState is null");
  }
  </code></pre>
  
  <p>When returning back the <code>MainActivity</code> from <code>Main2Activity</code>, I can see from the logging that <code>onCreate()</code>, <code>then onStart()</code>, then <code>onResume()</code> for <code>MainActivity</code> is called, then <code>onStop()</code> for <code>Main2Activity</code>. Unfortunatly the logging shows that <code>savedInstanceState</code> always comes back as null.</p>
  
  <p>To add to this, when in the emulator, switching the orientation back and forth causes this to work perfectly; <code>savedInstanceState</code> is not null and features the saved testInt.
  Thus I figure its a problem with my understanding and that there must be something im missing.</p>
  
  <p>My gradle has minSdkVersion set to 16, and targetSdkVersion set to 28. Am I maybe targeting too low a minSdkVersion?</p>
  
  <p>I have read through the "Understand the Activity Lifecycle" on the official android developer documentation but still cant get it.
  <a href="https://developer.android.com/guide/components/activities/activity-lifecycle" rel="nofollow noreferrer">https://developer.android.com/guide/components/activities/activity-lifecycle</a></p>
  
  <p>I did find similar problems but none of them match my situation exaclty, also the solutions they have suggested I am already doing anyway.</p>
  
  <p>Any insight would be greatly appreciated, thanks in advance.</p>