title: Retain fragment data on screen rotation
body: <p>I have 3 fragments where 1 fragment makes changes to the other 2.</p>
  <p>The problem is: When I rotate the screen background color and places of fragments 2 and 3 are restored.
  I want to keep them without modifying <code>AndroidManifest.xml</code>.
  Is it best to use onSaveInstanceState or setRetainInstance or both?</p>
  <p>I found some other answers about retaining fragments but none of them were beneficial for my case.</p>
  <p><strong>FirstFragment</strong></p>
  <pre><code>public class FirstFragment extends Fragment 
  {
      private OnFragmentInteractionListener mListener;
      private ChangeColorListener mCallBack;
      private boolean clickCheck = false;
  
      public FirstFragment() { }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment_first, container, false);
  
          Button buttonOk = v.findViewById(R.id.button_ok);
          Button btn = v.findViewById(R.id.button_replace);
          buttonOk.setOnClickListener(v1 -&gt; mCallBack.changeColor(R.color.black));
          btn.setOnClickListener(v12 -&gt; {
              clickCheck = !clickCheck;
              mListener.changeFragment(clickCheck);
          });
  
          return v;
      }
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          
          if (savedInstanceState!=null) {
              mCallBack =(MainActivity)getActivity();
              mListener = (MainActivity)getActivity();
          }
      }
  
      @Override
      public void onAttach(@NonNull Context context) {
          super.onAttach(context);
  
          if (context instanceof ChangeColorListener) {
              mCallBack = (ChangeColorListener) context;
          } else {
              throw new RuntimeException(context.toString()
                      + &quot; must implement ChangeColorListener&quot;);
          }
  
          try {
              mListener = (OnFragmentInteractionListener) context;
          } catch (ClassCastException e) {
              throw new ClassCastException(context.toString()
                      + &quot; must implement OnFragmentInteractionListener&quot;);
          }
      }
  
  
      @Override
      public void onDetach() {
          super.onDetach();
          mListener = null;
      }
  }
  </code></pre>
  <p><strong>SecondFragment</strong></p>
  <pre><code>public class SecondFragment extends Fragment 
  {
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_second, container, false);
      }
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setRetainInstance(true);
      }
  
      @Override
      public void onAttach(@NonNull Context context) {
          super.onAttach(context);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
      public void change(int color){
          getView().setBackgroundColor(color);
      }
  }
  </code></pre>
  <p><strong>ThirdFragment</strong></p>
  <pre><code>public class ThirdFragment extends Fragment {
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_third, container, false);
      }
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setRetainInstance(true);
  
      }
  
      @Override
      public void onAttach(@NonNull Context context) {
          super.onAttach(context);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
  
      public void change(int color){
          getView().setBackgroundColor(color);
      }
  }
  </code></pre>