title: Fragment to Fragment communication with FragmentStatePagerAdapter
body: <p>I'm trying to update the text of my TextView in my FragmentB through the Button in my FragmentA. But whenever I click the Button, nothing happens in my TextView. What could be the problem? Here's my code:</p>
  
  <p><strong>Communicator.java:</strong></p>
  
  <pre><code>public interface Communicator {
      public void respond(String data);
  }
  </code></pre>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements Communicator{
  
      ViewPager viewPager = null;
      FragmentManager fragmentManager = getSupportFragmentManager();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          viewPager = (ViewPager) findViewById(R.id.pager);   
          viewPager.setAdapter(new MyAdapter(fragmentManager));
  
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          FragmentB fragB = new FragmentB();
  
          fragmentTransaction.add(R.id.pager, fragB, "frag_tag");
          fragmentTransaction.commit();
  
      }
  
      public class MyAdapter extends FragmentStatePagerAdapter {
  
          public MyAdapter (FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int i) {
              Fragment fragment = null;
  
              if (i == 0)
              {
                  fragment = new FragmentA();
              }
              if (i == 1)
              {
                  fragment = new FragmentB();
              }
                      if (i == 2)
              {
                  fragment = new FragmentC();
              }
              if (i == 3)
              {
                  fragment = new FragmentD();
              }
                      if (i == 4)
              {
                  fragment = new FragmentE();
              }
              if (i == 5)
              {
              fragment = new FragmentF();
              }
              return fragment;
          }
  
          @Override
          public int getCount() {
              return 6;
          }
      }
  
      @Override
      public void respond(String data) {
          // TODO Auto-generated method stub
  
           FragmentB f2 =  (FragmentB) fragmentManager.findFragmentByTag("frag_tag");
           f2.changeText(data);
      }
  
  }
  </code></pre>
  
  <p><strong>FragmentA:</strong></p>
  
  <pre><code>public class FragmentA extends Fragment implements OnClickListener {
  
      int counter = 0;
      Button button1;
      Communicator comm;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.fragmenta, container, false);              
      }
  
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          comm = (Communicator) getActivity();
          button1 = (Button) getActivity().findViewById(R.id.button1);
          button1.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View arg0) {
          // TODO Auto-generated method stub
          counter++;
          comm.respond("The button was clicked" + counter + "times");
      }
  
  }
  </code></pre>
  
  <p><strong>FragmentB:</strong></p>
  
  <pre><code>public class FragmentB extends Fragment {
  
      TextView text1;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.fragmentb, container, false);              
      }
  
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          text1 = (TextView) getActivity().findViewById(R.id.textView1);
      }
  
      public void changeText(String data) {
          // TODO Auto-generated method stub
          text1.setText(data);
      }
  
  }
  </code></pre>