title: Unable to save Fragment state with onSaveInstanceState
body: <p>I have a <em>MainActivity</em> that manages two <strong>Fragments</strong>: <em>InputDataFragment</em> (is the one displayed at the opening of the app) which contains a series of <strong>EditText</strong> and <em>ResultsFragment</em>, which replaces <em>InputDataFragment</em> after a button is clicked.
  <br>
  I want to save the data in the <strong>EditText</strong> fields so that, even after configuration changes such as screen rotations, if I navigate back from <em>ResultsFragment</em> to <em>InputDataFragment</em>, I can use those data to fill them again. 
  <br>
  This is the code I use to navigatefrom <em>InputDataFragment</em> to <em>ResultsFragment</em>:</p>
  
  <pre><code>@Override
  public void onCalculate(String firstElement, String secondElement) {
  
      FragmentManager fragmentManager = getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  
      ResultsFragment resultsFragment = new ResultsFragment();
      fragmentTransaction.replace(R.id.container, resultsFragment);
      fragmentTransaction.commit();
  }
  </code></pre>
  
  <p>This is where I save the values in <em>InputDataFragment</em>   </p>
  
  <pre><code>@Override
  public void onSaveInstanceState(@NonNull Bundle outState) {
      super.onSaveInstanceState(outState);
  
      // Check if there's something to retain before putting it into outState Bundle
      if (!firstEditText.getText().toString().isEmpty()) {
          outState.putString("firstElement", firstEditText.getText().toString());
          Log.d(LOG_TAG, "Power saved");
      }
  
      if (!secondEditText.getText().toString().isEmpty()) {
          outState.putString("secondElement", secondEditText.getText().toString());
      }
  }
  </code></pre>
  
  <p>And this is where I retrieve them</p>
  
  <pre><code>@Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
      if (savedInstanceState != null) {
          firstEditText.setText(savedInstanceState.getString("firstElement"));
          secondEditText.setText(savedInstanceState.getString("secondElement"));
      }
  }
  </code></pre>
  
  <p>I've tried doing what was suggested in the accepted answers on these Stack Overflow questions, saving and restoring my Fragment references:
  <br><br>
  <a href="https://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack">Once for all, how to correctly save instance state of Fragments in back stack?</a> 
  <br><br>
  <a href="https://stackoverflow.com/questions/15608709/using-onsaveinstancestate-with-fragments-in-backstack">Using onSaveInstanceState with fragments in backstack?</a>
  <br><br></p>
  
  <pre><code>@Override
  protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
  
      getSupportFragmentManager().putFragment(outState, "dataFragment", dataFragment);
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      FragmentManager fragmentManager = getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  
      if (savedInstanceState != null) {
          dataFragment = (InputDataFragment) getSupportFragmentManager().getFragment(savedInstanceState, "dataFragment");
      } else {
          dataFragment = new InputDataFragment();
      }
  
      fragmentTransaction.add(R.id.container, dataFragment);
      fragmentTransaction.commit();
  }
  </code></pre>
  
  <p>The values are correctly present in <strong>Bundle</strong> when <code>onActivityCreated</code> is called in the <strong>Fragment</strong> because I've tried to Log them</p>
  
  <p>The problem is that I get an error in the line where I call <code>putFragment</code> within <code>onSaveInstanceState</code>, when I'm in <em>ResultFragment</em> and I rotate the device.</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.mycompany.myproject, PID: 20161
  java.lang.IllegalStateException: Fragment InputDataFragment{679fd26} is not currently in the FragmentManager
      at androidx.fragment.app.FragmentManagerImpl.putFragment(FragmentManager.java:923)
      at com.mycompany.myproject.MainActivity.onSaveInstanceState(MainActivity.java:21)
      at android.app.Activity.performSaveInstanceState(Activity.java:1646)
      at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1508)
      at android.app.ActivityThread.callActivityOnSaveInstanceState(ActivityThread.java:5476)
      at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4792)
      at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5424)
      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5342)
      at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2155)
      at android.os.Handler.dispatchMessage(Handler.java:109)
      at android.os.Looper.loop(Looper.java:207)
      at android.app.ActivityThread.main(ActivityThread.java:7539)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
  </code></pre>