title: Sending data from Fragment to Fragment using Interface, by setting Fragment Layouts dynamically. Saved instance of Fragment NOT working
body: <p>I am sending data from one fragment to another, using interface. This works absolutely fine. Now I am trying to use onSaveInstanceState() to save the value in 
  a Fragment and retrieving in onCreate(). however i'm getting null in Bundle of onCreate(). P.S. everything works fine when i set the Fragment directly into the activity layout in xml. but when i set the fragment through java code into the activity, the onSaveInstanceState is failing. It is not saving the last known value. Please help.  Pasting the code below. You can try it out to know the exact issue.</p>
  
  <p><strong>Fragment_A :</strong> </p>
  
  <pre><code>public class Fragment_A extends Fragment implements View.OnClickListener {
  
  Button btnAdd;
  int counter = 0;
  Communicator comm;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if(savedInstanceState != null){
          counter = savedInstanceState.getInt("counter", 0);
      }
  }
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_a, container, false);
      return view;
  }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      comm = (Communicator) getActivity();
      btnAdd = (Button) getActivity().findViewById(R.id.btnAdd);
      btnAdd.setOnClickListener(this);
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putInt("counter", counter);
  }
  
  @Override
  public void onClick(View v) {
      counter++;
      comm.sendData(counter);
  }
  }
  
  
  interface Communicator{
      void sendData(int i);
  }
  </code></pre>
  
  <p>=====</p>
  
  <p><strong>Activity :</strong> </p>
  
  <pre><code>public class Activity_InterComm extends Activity implements Communicator{
  
  FragmentManager manager;
  FragmentTransaction transaction;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_intercomm);
  
      Fragment_A fragment_1 = new Fragment_A();
      Fragment_B fragment_2 = new Fragment_B();
  
      manager = getFragmentManager();
      transaction = manager.beginTransaction();
  
      transaction.add(R.id.frag_a, fragment_1, "Frag1");
      transaction.add(R.id.frag_b, fragment_2, "Frag2");
      transaction.commit();
  }
  
  @Override
  public void sendData(int i) {
      manager = getFragmentManager();
      Fragment_B fragment_b = (Fragment_B) manager.findFragmentById(R.id.frag_b);
      fragment_b.setData("Button has been clicked " + i + " times");
  }
  }
  </code></pre>
  
  <p>=====</p>
  
  <p><strong>Fragment_B :</strong></p>
  
  <pre><code>public class Fragment_B extends Fragment {
  
  TextView txtMessage;
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_b, container, false);
      return view;
  }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      txtMessage = (TextView) getActivity().findViewById(R.id.txtMessage);
  }
  
  public void setData(String message){
      txtMessage.setText(message);
  }
  }
  </code></pre>
  
  <p><strong>activity_intercomm.xml :</strong> </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"&gt;
  
  &lt;!--&lt;fragment
      android:id="@+id/frag_a"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_marginBottom="8dp"
      android:layout_weight="1"
      android:name="android.learning.com.fragintercomm.Fragment_A"/&gt;
  
  &lt;fragment
      android:id="@+id/frag_b"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:name="android.learning.com.fragintercomm.Fragment_B"/&gt;--&gt;
  
  &lt;LinearLayout
      android:id="@+id/frag_a"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_marginBottom="8dp"
      android:layout_weight="1"
      android:background="@color/colorAccent"
      android:orientation="vertical"/&gt;
  
  &lt;LinearLayout
      android:id="@+id/frag_b"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_marginBottom="8dp"
      android:layout_weight="1"
      android:background="@color/colorPrimaryDark"
      android:orientation="vertical"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>