title: Retrieve values from fragment on swipe in ViewPager - Android
body: <p>I am trying to create a registration screens using <code>ViewPager</code> where user will give all the details and clicks <code>Submit</code> button in the last fragment.  </p>
  
  <p>On swiping each fragment I have to validate the data and have save it to an object in Activity which has the <code>ViewPager</code>. I can't find a perfect way to do that. </p>
  
  <p>Below is my code snippet</p>
  
  <p><strong>RegistrationActivity.java</strong></p>
  
  <pre><code>public class RegistrationActivity extends FragmentActivity implements MultiPurposeDialogListener {
  
  private ViewPager viewPager;
  private static final int NUM_PAGES = 4;
  private PagerAdapter mPagerAdapter;
  private boolean isActivityVisible;
  private ProgressDialog progressDialog;
  
  // Have to save data in these object
  public Employee Employee = newEmployee();
  public EmpDetails empDetails = new EmpDetails();
  public Devices devices = new Devices();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_registration);
  
      viewPager = (ViewPager) findViewById(R.id.pager);
  
      mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
      viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
      viewPager.setAdapter(mPagerAdapter);
  }
  
  public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
      public ScreenSlidePagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          return RegistrationSlideFragment.create(position);
      }
  
      @Override
      public int getCount() {
          return NUM_PAGES;
      }
  }
  }
  </code></pre>
  
  <p><strong>RegistrationSlideFragment.java</strong></p>
  
  <pre><code>    public static RegistrationSlideFragment create(int pageNumber) {
      RegistrationSlideFragment fragment = new RegistrationSlideFragment();
      Bundle args = new Bundle();
      args.putInt(ARG_PAGE, pageNumber);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mPageNumber = getArguments().getInt(ARG_PAGE);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
  
      ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_registraion_slide, container, false);
  
      TextView tx = (TextView) viewGroup.findViewById(R.id.tv_heading);
      LinearLayout ln_slide1 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide1);
      LinearLayout ln_slide2 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide2);
      LinearLayout ln_slide3 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide3);
      LinearLayout ln_slide4 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide4);
      Button btn_leftarrow = (Button) viewGroup.findViewById(R.id.btn_leftarrow);
      Button btn_rightarrow = (Button) viewGroup.findViewById(R.id.btn_rightarrow);
      Button btn_submit = (Button) viewGroup.findViewById(R.id.btn_submit);
  
  
      if ((mPageNumber + 1) == 1) {
          btn_leftarrow.setVisibility(View.INVISIBLE);
          ed_dob = (EditText) viewGroup.findViewById(R.id.et_dob);
          ed_fname = (EditText) viewGroup.findViewById(R.id.et_dob);
          ed_lname = (EditText) viewGroup.findViewById(R.id.et_dob);
          ed_email = (EditText) viewGroup.findViewById(R.id.et_dob);
          ed_country = (EditText) viewGroup.findViewById(R.id.et_dob);
  
          ed_dob.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  DialogFragment newFragment = new SelectDateFragment();
                  FragmentManager fm = getActivity().getFragmentManager();
                  newFragment.show(fm, "DatePicker");
              }
          });
  
          tx.setText("Personal Details");
          ln_slide1.setVisibility(View.VISIBLE);
  
      } else if ((mPageNumber + 1) == 2) {
          sp_availability = (Spinner) viewGroup.findViewById(R.id.sp_availability);
          ArrayAdapter&lt;CharSequence&gt; adapter_availability = ArrayAdapter.createFromResource(getActivity(),
                  R.array.proj_availability, android.R.layout.simple_spinner_item);
          sp_availability.setAdapter(adapter_availability);
  
  
          chk_tot_performance = (CheckBox) viewGroup.findViewById(R.id.chk_tot_performance);
          chk_tot_functional = (CheckBox) viewGroup.findViewById(R.id.chk_tot_functional);
          chk_tot_localozation = (CheckBox) viewGroup.findViewById(R.id.chk_tot_localization);
          chk_tot_security = (CheckBox) viewGroup.findViewById(R.id.chk_tot_security);
          chk_tom_native = (CheckBox) viewGroup.findViewById(R.id.chk_tom_native);
          chk_tom_hybrid = (CheckBox) viewGroup.findViewById(R.id.chk_tom_hybrid);
          chk_tom_web = (CheckBox) viewGroup.findViewById(R.id.chk_tom_web);
  
  
          tx.setText("Project Prefetance");
          ln_slide2.setVisibility(View.VISIBLE);
  
      } else if ((mPageNumber + 1) == 3) {
          sp_functional = (Spinner) viewGroup.findViewById(R.id.sp_functional);
          ArrayAdapter&lt;CharSequence&gt; adapter_functional = ArrayAdapter.createFromResource(getActivity(),
                  R.array.testing_expertise, android.R.layout.simple_spinner_item);
          sp_functional.setAdapter(adapter_functional);
  
          sp_security = (Spinner) viewGroup.findViewById(R.id.sp_security);
          ArrayAdapter&lt;CharSequence&gt; adapter_security = ArrayAdapter.createFromResource(getActivity(),
                  R.array.testing_expertise, android.R.layout.simple_spinner_item);
          sp_security.setAdapter(adapter_security);
  
          sp_performance = (Spinner) viewGroup.findViewById(R.id.sp_performance);
          ArrayAdapter&lt;CharSequence&gt; adapter_performance = ArrayAdapter.createFromResource(getActivity(),
                  R.array.testing_expertise, android.R.layout.simple_spinner_item);
          sp_performance.setAdapter(adapter_performance);
  
          sp_localization = (Spinner) viewGroup.findViewById(R.id.sp_localization);
          ArrayAdapter&lt;CharSequence&gt; adapter_localization = ArrayAdapter.createFromResource(getActivity(),
                  R.array.testing_expertise, android.R.layout.simple_spinner_item);
          sp_localization.setAdapter(adapter_localization);
  
  
          tx.setText("Test Expertise");
          ln_slide3.setVisibility(View.VISIBLE);
      } else if ((mPageNumber + 1) == 4) {
          btn_rightarrow.setVisibility(View.INVISIBLE);
          btn_submit.setVisibility(View.VISIBLE);
          sp_network = (Spinner) viewGroup.findViewById(R.id.sp_network_type);
          ArrayAdapter&lt;CharSequence&gt; adapter_networkstatus = ArrayAdapter.createFromResource(getActivity(),
                  R.array.networktype, android.R.layout.simple_spinner_item);
          sp_network.setAdapter(adapter_networkstatus);
  
          sp_manufaturer = (Spinner) viewGroup.findViewById(R.id.sp_manufacturer);
          sp_device_name = (Spinner) viewGroup.findViewById(R.id.sp_device_name);
          sp_version = (Spinner) viewGroup.findViewById(R.id.sp_os_version);
          sp_manufaturer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  setDeviceList("devicecompactability_devicelist.json", parent.getItemAtPosition(position).toString());
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
              }
          });
  
  
          tx.setText("Your Device Details");
          ln_slide4.setVisibility(View.VISIBLE);
          setManufacturerList("devicecompactability_devicelist.json");
          setVersionList("devicecompactability_platformversion.json");
  
      }
  
  
      btn_submit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ArrayList&lt;Object&gt; finalObject = new ArrayList&lt;Object&gt;();
              finalObject.add(registrationActivity.Employee);
              finalObject.add(registrationActivity.empDetails);
              finalObject.add(registrationActivity.devices);
  
              Gson gson = new Gson();
              String resultJson = gson.toJson(finalObject);
              System.out.println(resultJson);
          }
      });
  
  
      return viewGroup;
  }
  </code></pre>