title: How to pass a string between fragments in a viewpager
body: <p>I'm trying to pass a string between two fragments in a viewpager but I didn't found the right way to do it.
  Here is my code so far :</p>
  
  <pre><code>public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
  
      final int PAGE_COUNT = 6;
  
      /** Constructor of the class */
      public MyFragmentPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      /** This method will be invoked when a page is requested to create */
      @Override
      public Fragment getItem(int arg0) {
  
  
          switch (arg0) {
          case 0:
              return new MyFragment();
          case 1:
              return new part2();
          case 2:
              return new Part3();
          case 3:
              return new Part4();
          case 4:
              return new Part5();
          case 5:
              return new Part6();
          default:
              return null;
          }
      }
  
      /** Returns the number of pages */
      @Override
      public int getCount() {     
          return PAGE_COUNT;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {        
          return "Page #" + ( position + 1 );
      }
  
  
  
  
  
  }
  
  public class MainActivity extends FragmentActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          /** Getting a reference to the ViewPager defined the layout file */        
          ViewPager pager = (ViewPager) findViewById(R.id.pager);
  
          /** Getting fragment manager */
          FragmentManager fm = getSupportFragmentManager();
  
          /** Instantiating FragmentPagerAdapter */
          MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
  
          /** Setting the pagerAdapter to the pager object */
          pager.setAdapter(pagerAdapter);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  }
  </code></pre>
  
  <p>I'm not using tabs with the viewpager. What should I write in the fragments?</p>
  
  <p><strong>UPDATE</strong> :
  I tried to implement an interface like suggested in the answers : </p>
  
  <pre><code>public interface OnTogglePressListener {
      public void onTogglePressed(String msg);
  }
  </code></pre>
  
  <p>On the first fragment :</p>
  
  <pre><code>@Override
  public void onAttach(Activity activity) {
          // TODO Auto-generated method stub
          try {
              buttonListener = (OnTogglePressListener) getActivity();
          } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() + " must implement onTogglePressed");
          }
          super.onAttach(activity);
      }
  
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          buttonListener.onTogglePressed("Off");
  ... }
  </code></pre>
  
  <p>On the second fragment :</p>
  
  <pre><code>public String getEtat(String etat)
      {   state =etat;
          return etat;
  
      }
  </code></pre>
  
  <p>On the main activity : </p>
  
  <pre><code>public void onTogglePressed(String etat)
      {
          Part5 p = (Part5)getSupportFragmentManager().findFragmentById(R.layout.frag_2);
          p.getEtat(etat);
  
      }
  </code></pre>
  
  <p>But I got a NullPointerException everytime.</p>