title: Saving and restoring state using fragments
body: <p>I'm trying to understand the process of saving and restoring state using fragments. I've created sliding navigation menu using it.</p>
  
  <p>In one of the fragments there is this code: </p>
  
  <pre><code>public class FifthFragment extends Fragment {
  
      CheckBox cb;
      View view;
  
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
          view = inflater.inflate(R.layout.fifth_layout, container, false);
          cb = (CheckBox) view.findViewById(R.id.checkBox);
          return view;
  
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          if (savedInstanceState != null) {
              // Restore save state
          }
  
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          // save state
      }
  
  }
  </code></pre>
  
  <p>For example I want to save the state of the CheckBox before user exits the fragment and restore it when the fragment is created again. How to achieve this? </p>
  
  <p>EDIT: </p>
  
  <p>According to raxellson's answer I've changed my fragment to this:</p>
  
  <pre><code>public class FifthFragment extends Fragment {
  
      private static final String CHECK_BOX_STATE = "string";
      CheckBox cb;
      View view;
  
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fifth_layout, container, false);
  
          cb = (CheckBox) view.findViewById(R.id.checkBox);
          if (savedInstanceState == null) {
              Log.i("statenull", "null");
          }
          if (savedInstanceState != null) {
              // Restore last state for checked position.
              boolean checked = savedInstanceState.getBoolean(CHECK_BOX_STATE, false);
              cb.setChecked(checked);
          }
  
          return view;
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putBoolean(CHECK_BOX_STATE, cb.isChecked());
      }
  
  }
  </code></pre>
  
  <p>I got logged <code>I/statenull: null</code> so savedInstanceState was not saved. What am I doing wrong?</p>