title: Problems with getSavedInstanceState() and onViewStateRestored() bundle
body: <p>I have a Fragment in an Activity and when the device is rotated, onSaveInstanceState() and onViewStateRestored() are called.</p>
  
  <p>onSaveInstanceState():</p>
  
  <pre><code>@Override
  public void onSaveInstanceState(Bundle outState){
      Log.e("Fragment", "Called onSaveInstanceState");
  
      outState = getSavedInstanceState();
  
      super.onSaveInstanceState(outState);
  }
  </code></pre>
  
  <p>This is what sets the outState bundle in onSaveInstanceState():</p>
  
  <pre><code>public static Bundle getSavedInstanceState(){
      Bundle saveState = new Bundle();
  
      //Save view states
      saveState.putBoolean("startButtonVisible", buttonStart.getVisibility() == View.VISIBLE);
      saveState.putBoolean("stopButtonVisible", buttonStop.getVisibility() == View.VISIBLE);
      saveState.putBoolean("resetButtonVisible", buttonReset.getVisibility() == View.VISIBLE);
      saveState.putBoolean("saveButtonVisible", buttonSave.getVisibility() == View.VISIBLE);
      saveState.putBoolean("openLatestFileButtonVisible", buttonOpenLatestFile.getVisibility() == View.VISIBLE);
  
      saveState.putBoolean("editTextStartValueVisible", editTextStartValue.isEnabled());
      saveState.putBoolean("buttonStartValueDownVisible", buttonStartValueDown.isEnabled());
      saveState.putBoolean("buttonStartValueUpVisible", buttonStartValueUp.isEnabled());
      saveState.putBoolean("editTextEndValueVisible", editTextEndValue.isEnabled());
      saveState.putBoolean("buttonEndValueDownVisible", buttonEndValueDown.isEnabled());
      saveState.putBoolean("buttonEndValueUpVisible", buttonEndValueUp.isEnabled());
  
      return saveState;
  }
  </code></pre>
  
  <p>This saves the data correctly however when onViewStateRestored() is called, the data is incorrect.</p>
  
  <pre><code>@Override
  public void onViewStateRestored(Bundle savedInstanceState){
      Log.e("Fragment", "Called onViewStateRestored");
  
      super.onViewStateRestored(savedInstanceState);
  
      //Restore view states
      if (savedInstanceState != null){
  
          restoreInstanceState(savedInstanceState);
  
          if (buttonReset.getVisibility() == View.VISIBLE){
              updateUi();
          }
      }
  }
  </code></pre>
  
  <p>When onViewStateRestored() is called, it also calls restoreInstanceState() which is what is supposed to restore the state of each view.</p>
  
  <pre><code>public static void restoreInstanceState(Bundle savedInstanceState){
      buttonStart.setVisibility(savedInstanceState.getBoolean("startButtonVisible") ? View.VISIBLE : View.INVISIBLE);
      buttonStop.setVisibility(savedInstanceState.getBoolean("stopButtonVisible") ? View.VISIBLE : View.INVISIBLE);
      buttonReset.setVisibility(savedInstanceState.getBoolean("resetButtonVisible") ? View.VISIBLE : View.INVISIBLE);
      buttonSave.setVisibility(savedInstanceState.getBoolean("saveButtonVisible") ? View.VISIBLE : View.INVISIBLE);
      buttonOpenLatestFile.setVisibility(savedInstanceState.getBoolean("openLatestFileButtonVisible") ? View.VISIBLE : View.INVISIBLE);
  
      editTextStartValue.setEnabled(savedInstanceState.getBoolean("editTextStartValueVisible"));
      buttonStartValueDown.setEnabled(savedInstanceState.getBoolean("buttonStartValueDownVisible"));
      buttonStartValueDown.setImageResource(buttonStartValueDown.isEnabled() ? R.drawable.ic_arrow_down : R.drawable.ic_arrow_down_gray);
      buttonStartValueUp.setEnabled(savedInstanceState.getBoolean("buttonStartValueUpVisible"));
      buttonStartValueUp.setImageResource(buttonStartValueUp.isEnabled() ? R.drawable.ic_arrow_up : R.drawable.ic_arrow_up_gray);
      editTextEndValue.setEnabled(savedInstanceState.getBoolean("editTextEndValueVisible"));
      buttonEndValueDown.setEnabled(savedInstanceState.getBoolean("buttonEndValueDownVisible"));
      buttonEndValueDown.setImageResource(buttonEndValueDown.isEnabled() ? R.drawable.ic_arrow_down : R.drawable.ic_arrow_down_gray);
      buttonEndValueUp.setEnabled(savedInstanceState.getBoolean("buttonEndValueUpVisible"));
      buttonEndValueUp.setImageResource(buttonEndValueUp.isEnabled() ? R.drawable.ic_arrow_up : R.drawable.ic_arrow_up_gray);
  
      Log.e("Fragment", "Restored view visibility");
  }
  </code></pre>
  
  <p>Why is the data incorrect when onViewStateRestored() is called? All of the views just become invisible regardless of what the they were before. Are the bundles not the same when saving and restoring the states?</p>
  
  <p>All of this code is however working when calling <code>fragmentManager.beginTransaction().replace(R.id.content_frame, CustomFragmentManager.savedFilesFragment).commit();</code> and then calling onViewStateRestored() manually.</p>
  
  <p><strong>EDIT:</strong> Changed field names.</p>
  
  <p><strong>Solution:</strong> I have replaced <code>outState = new Bundle(getSavedInstanceState());</code> with <code>outState.putAll(getSavedInstanceState());</code> and it is now working. Calling <code>setRetainInstance(true);</code> was not necessary.</p>