title: Android: Passing Objects Between Fragments
body: <p>Before i start, i have look through question such as:</p>
  
  <p><a href="https://stackoverflow.com/questions/18253666/passing-data-between-fragments-screen-overlap">Passing data between fragments: screen overlap</a>
  <a href="https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments">How to pass values between Fragments</a></p>
  
  <p>as well as Android docs:</p>
  
  <p><a href="http://developer.android.com/training/basics/fragments/communicating.html" rel="nofollow noreferrer">http://developer.android.com/training/basics/fragments/communicating.html</a></p>
  
  <p>as well as this article:</p>
  
  <p><a href="http://manishkpr.webheavens.com/android-passing-data-between-fragments/" rel="nofollow noreferrer">http://manishkpr.webheavens.com/android-passing-data-between-fragments/</a></p>
  
  <p>Though all the cases mentioned above similar to what i have, it is not entirely identical. I followed a good tutorial here (Some portion of my code is based on this article):</p>
  
  <p><a href="http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/" rel="nofollow noreferrer">http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/</a></p>
  
  <p>I have the following files:</p>
  
  <p><strong>RegisterActivity.java<br>
  NonSwipeableViewPager.java<br>
  ScreenSliderAdapter.java<br>
  RegisterOneFragment.java<br>
  RegisterTwoFragment.java</strong>   </p>
  
  <p>And the following layouts:</p>
  
  <p><strong>activity_register.xml<br>
  fragment_register_one.xml<br>
  fragment_register_two.xml</strong></p>
  
  <p>What i am trying to achieve is passing an Serializable object from RegisterFragmentOne to RegisterFragmentTwo.</p>
  
  <p>So far this is what i have done (some codes are omitted):</p>
  
  <p><strong>RegisterActivity.java</strong></p>
  
  <pre><code>public class RegisterActivity extends FragmentActivity
               implements RegisterOneFragment.OnEmailRegisteredListener{
  
      public static NonSwipeableViewPager viewPager;
      private ScreenSliderAdapter mAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_register);
  
          // Initilization
          mAdapter = new ScreenSliderAdapter(getSupportFragmentManager());
          viewPager = (NonSwipeableViewPager) findViewById(R.id.pager);
          viewPager.setAdapter(mAdapter);
      }
  
      public void onEmailRegistered(int position, Registration regData){
          Bundle args = new Bundle();
          args.putSerializable("regData", regData);
          viewPager.setCurrentItem(position, true);
      }
  }
  </code></pre>
  
  <p><strong>ScreenSliderAdapter.java</strong></p>
  
  <pre><code>public class ScreenSliderAdapter extends FragmentPagerAdapter{
  
      public ScreenSliderAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int index) {
  
          switch (index) {
          case 0:
              return new RegisterOneFragment();
          case 1:
              return new RegisterTwoFragment();
          case 2:
              return new RegisterThreeFragment();
          }
  
          return null;
      }
  
      @Override
      public int getCount() {
          return 3;
      }
  }
  </code></pre>
  
  <p><strong>NonSwipeableViewPager.java</strong> (extending ViewPager class, and overrides the following)</p>
  
  <pre><code>@Override
  public boolean onInterceptTouchEvent(MotionEvent arg0) {
      // Never allow swiping to switch between pages
      return false;
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      // Never allow swiping to switch between pages
      return false;
  }
  </code></pre>
  
  <p><strong>RegisterOneFragment.java</strong></p>
  
  <pre><code>public class RegisterOneFragment extends Fragment {
      OnEmailRegisteredListener mCallBack;
      public interface OnEmailRegisteredListener {
          /** Called by RegisterOneFragment when an email is registered */
          public void onEmailRegistered(int position, Registration regData);
      }
  
  public void onAttach(Activity activity){
      super.onAttach(activity);
  
      // This makes sure that the container activity has implemented
      // the callback interface. If not, it throws an exception.
      try {
          mCallBack = (OnEmailRegisteredListener) activity;
      } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString()
                  + " must implement OnEmailRegisteredListener");
      }
  }
  
  ... And some to execute some HTTP request via separate thread...
  }
  </code></pre>
  
  <p>What i am trying to accomplish is that when ever a user pressed a button on RegisterOneFragment, a data will be sent to a server (and returns some validation over JSON). If the returned data is valid, the the application should go to the next fragment which is RegistrationTwoFragment.</p>
  
  <p>I am having some confusion as how to pass objects between fragments, since my Fragments is created using an Adapter. And that Adapter is then attached to my Activity.</p>
  
  <p>Can anyone help me with this? Thx</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>I tried to make a shortcut (unfortunately does not work) like so:</p>
  
  <p>In RegisterActivity i created:</p>
  
  <pre><code>public Registration regData;
  </code></pre>
  
  <p>and in RegisterOneFragment:</p>
  
  <pre><code>/* PLACED ON POST EXECUTE */
  ((RegisterActivity)getActivity()).regData = regData;
  </code></pre>
  
  <p>Finally called it in RegisterTwoFragment</p>
  
  <pre><code>Registration regData;
  regData = ((RegisterActivity) getActivity()).regData;
  </code></pre>
  
  <p>It throws a nullPointerExceptions</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Just to be clear, RegisterActivty contains multiple fragments. And the only way user can navigate between fragment is by clicking a button. The Activity has no Tab bar.</p>