Send data from view pager to activity
<p>I have an application that contains a 3-steps register activity. Those 3 steps are divided into 3 fragments with veiwpager to swipe back and forward and in the last step there is a register button that when pressed the data will be sent to a database which will be done in the <code>Register</code> Activity.  </p>

<p>I'm facing a problem in passing all the data the user enters in the 3 fragments to the <code>Register</code> Activity.  </p>

<p>I was thinking on saving the data of each fragment when swiping to the next fragment but i got confused!  </p>

<p>Register Activity:</p>

<pre><code>public class Register extends FragmentActivity {
  ViewPager pager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new PageAdapter(getSupportFragmentManager()));

    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
      // This method will be invoked when a new page becomes selected.
      @Override
      public void onPageSelected(int position) {

      }

      // This method will be invoked when the current page is scrolled
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      // Called when the scroll state changes:
      // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
      @Override
      public void onPageScrollStateChanged(int state) {
        // Code goes here
      }
    });
  }

  private static String makeFragmentName(int viewPagerId, int index) {
    return "android:switcher:" + viewPagerId + ":" + index;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_register, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
}
</code></pre>

<p>Register XML:</p>

<pre><code>&lt;android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&gt;
&lt;android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:paddingBottom="4dp"
    android:paddingTop="4dp" /&gt;

&lt;/android.support.v4.view.ViewPager&gt;
</code></pre>

<p>First Fragment:</p>

<pre><code>public class personal_info_fragment extends Fragment {

  private EditText fname,lname,pass,conf_pass,phone_nb,license_nb;
  private Spinner age_spinner;
  private RadioButton male_rb,female_rb;
  private String first_name,last_name,license,phone,password,conf_password;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.personal_info_fragment, container, false);

    fname = (EditText) v.findViewById(R.id.fname);
    lname = (EditText) v.findViewById(R.id.lname);
    license_nb = (EditText) v.findViewById(R.id.license_nb);
    pass = (EditText) v.findViewById(R.id.pass);
    conf_pass = (EditText) v.findViewById(R.id.conf_pass);
    phone_nb = (EditText) v.findViewById(R.id.phone_nb);

    return v;
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  }

  public static personal_info_fragment newInstance(String text) {

    personal_info_fragment f = new personal_info_fragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
  }
}
</code></pre>

<p>First Fragment XML:</p>

<pre><code>&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;TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/personal_info"
        android:id="@+id/textView3"
        android:gravity="center" /&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fname"
        android:hint="@string/fname"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lname"
        android:layout_gravity="center_horizontal"
        android:hint="@string/lname"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/license_nb"
        android:layout_gravity="center_horizontal"
        android:hint="@string/license_nb"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/pass"
        android:layout_gravity="center_horizontal"
        android:hint="@string/pass"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/conf_pass"
        android:layout_gravity="center_horizontal"
        android:hint="@string/conf_pass"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/phone_nb"
        android:layout_gravity="center_horizontal"
        android:hint="@string/phone"/&gt;

    &lt;RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:orientation="horizontal"&gt;

        &lt;RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male"
            android:id="@+id/male_rb" /&gt;

        &lt;RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female"
            android:id="@+id/female_rb" /&gt;
    &lt;/RadioGroup&gt;

    &lt;Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/age_spinner" /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>the second and the third fragments is similar to first fragment.</p>

<p>Page Adapter:</p>

<pre><code>public class PageAdapter extends FragmentPagerAdapter {
  public PageAdapter(FragmentManager fm) {
    super(fm);
  }

  @Override
  public Fragment getItem(int pos) {
    switch(pos) {
      case 0:
        return new personal_info_fragment();
      case 1:
        return new medical_info_fragment();
      case 2:
        return new car_info_fragment();
      default:
        return null;
    }
  }

  @Override
  public int getCount() {
    return 3;
  }

  @Override
  public CharSequence getPageTitle(int position) {
    switch(position) {
      case 0:
        return "Personal Information";
      case 1:
        return "Medical Information";
      case 2:
        return "Car Information";
      default:
        return null;
    }
  }
}
</code></pre>

<p>THANKS in advance.</p>