title: How to pass data from one fragment to another in View Pager
body: <p>How do I pass the data from one fragment to another when button is pressed from the activity in which view pager is initiate. </p>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;android.support.v4.view.ViewPager
          android:elevation="@dimen/activity_horizontal_margin"
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
      &lt;/android.support.v4.view.ViewPager&gt;
  
  
      &lt;Button
          android:id="@+id/btn_next"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:background="@null"
          android:text="@string/next"
          android:textColor="@android:color/white" /&gt;
  </code></pre>
  
  <p>when I press btn_next then next fragment will show. How will I fetch the data from previous fragment to current fragment?</p>
  
  <p>I have one fragment:</p>
  
  <pre><code>public class BpDetails extends Fragment {
  
          private int page;
  
      private EditText systolic;
      private EditText diastolic;
  
      // newInstance constructor for creating fragment with arguments
          public static BpDetails newInstance(int page) {
              BpDetails fragmentFirst = new BpDetails();
              Bundle args = new Bundle();
              args.putInt("someInt", page);
              fragmentFirst.setArguments(args);
              return fragmentFirst;
          }
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              page = getArguments().getInt("someInt", 0);
          }
  
  
          // Inflate the view for the fragment based on layout XML
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              final View view = inflater.inflate(R.layout.bp_details, container, false);
  
              Log.i("View ",view.toString());
  
  
              systolic =(EditText) view.findViewById(R.id.systolic);
              diastolic =(EditText) view.findViewById(R.id.diastolic);
  
  
              return view;
          }
  }
  </code></pre>
  
  <p>and page adapter</p>
  
  <pre><code>public static class MyPagerAdapter extends FragmentPagerAdapter {
          private static int NUM_ITEMS = 4;
          private static int mSelectedPosition;
  
          public MyPagerAdapter(FragmentManager fragmentManager) {
              super(fragmentManager);
              //mSelectedPosition=selectedPosition;
          }
  
          // Returns total number of pages
          @Override
          public int getCount() {
              return NUM_ITEMS;
          }
  
          // Returns the fragment to display for that page
          @Override
          public Fragment getItem(int position) {
              switch (position) {
                  case 0: // Fragment # 0 - This will show FirstFragment
                      return BasicDetails.newInstance(0);
                  case 1:
                      return BpDetails.newInstance(1);
                  case 2:
                      return BslDetails.newInstance(2);
                  case 3:
                      return Summary.newInstance(3);
  
                  default:
                      return null;
              }
          }
  
      }
  </code></pre>
  
  <p>For just clarification I want to access the systolic value in another fragment when user click on btn_next in activity. </p>
  
  <p>Update:</p>
  
  <p>I have try to do this way 
  in Fragment. </p>
  
  <pre><code>  public String getSystolic(){
  
              return this.systolic.getText().toString();
  
          }
  </code></pre>
  
  <p>then when user click on next button</p>
  
  <pre><code>Fragment bpFragment=adapterViewPager.getItem(vpPager.getCurrentItem());
  
                      if (bpFragment instanceof BpDetails){
  
                          System.out.println("Current item is "+vpPager.getCurrentItem());
  
                          Fragment f=adapterViewPager.getInstantiatedFragment(vpPager.getCurrentItem());
  
  
                         BpDetails fr = (BpDetails)adapterViewPager.getItem(vpPager.getCurrentItem());
  
                          String systolicString = fr.getSystolic();
  
                          Log.i("Systolic is ",systolicString);
                      }
  </code></pre>