title: how to share data between ViewPager fragments?
body: <p>I just started with android programming, therefore my code looks pretty ugly. It is cobbled together from various tutorials and almost does what I need it to. A quick explanation what I need it to do:</p>
  
  <p>I have an activity with two tabs. One tab implements an OnClickListener on the whole screen which increments a counter every time the screen is clicked. If the screen is swiped from top to bottom, or from bottom to top, that counter is reset to 0. Now, what I have been struggling with: When I swipe to the next tab, I want the current value of that counter to be passed to the next fragment where it shall be used for calculations. </p>
  
  <p>Can anyone help me out here?</p>
  
  <p>I will also happily accept any comments that help me clean up my current code.</p>
  
  <p>Cheers</p>
  
  <p>Here is my current code:</p>
  
  <p>Main Activity's xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &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;de.regenistdoof.sporecounter.SlidingTabLayout
          android:id="@+id/sliding_tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" /&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="0px"
          android:layout_weight="1"
          android:background="@android:color/white" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Main Activity:</p>
  
  <pre><code>package de.regenistdoof.sporecounter;
  
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.view.ViewPager;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  
  public class MainActivity extends FragmentActivity {
  
      ViewPager viewPager;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Get the ViewPager and set it's PagerAdapter so that it can display items
          viewPager = (ViewPager) findViewById(R.id.viewpager);
          viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
                  MainActivity.this));
  
  
  
          // Give the SlidingTabLayout the ViewPager
          SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
          // Center the tabs in the layout
          slidingTabLayout.setDistributeEvenly(true);
          slidingTabLayout.setViewPager(viewPager);
      }
  
  
      @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_main, 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>Counter Fragment's xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- This is the fragment  to be displayed for a section associated with a tab --&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/layout"&gt;
  
      &lt;TextView
          android:id="@+id/counter"
          android:text="@string/counter_default"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:layout_gravity="center_horizontal"
          android:textSize="50sp" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Counter Fragment code:</p>
  
  <pre><code>package de.regenistdoof.sporecounter;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.GestureDetector;
  import android.view.LayoutInflater;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  // In this case, the fragment displays simple text based on the page
  public class FragmentCounter extends Fragment implements View.OnClickListener,GestureDetector.OnGestureListener{
  
  int count = 0;
  TextView counter;
  LinearLayout layout;
  View view;
  boolean swipe = false;
  String TAG = "sporecounter";
  
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          view = inflater.inflate(R.layout.fragment_counter, container, false);
  
          layout = (LinearLayout) view.findViewById(R.id.layout);
          layout.setOnClickListener(this);
  
          counter = (TextView) view.findViewById(R.id.counter);
  
          final GestureDetector gesture = new GestureDetector(getActivity(),
                  new GestureDetector.SimpleOnGestureListener() {
  
  
                      @Override
                      public boolean onSingleTapUp(MotionEvent e) {
  
                          return false;
                      }
  
                      @Override
                      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                             float velocityY) {
  
                          final int SWIPE_MIN_DISTANCE = 300;
  
                          try {
                              if (Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MIN_DISTANCE &amp;&amp; e1.getY() &gt; e2.getY()){
                                  Log.d(TAG, "swipe up");
                                  count = 0;
                                  swipe = true;
                                  counter.setText(Integer.toString(count));
                              }
                              else if ((Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MIN_DISTANCE &amp;&amp; e2.getY() &gt; e1.getY())){
                                  Log.d(TAG, "swipe down");
                                  count = 0;
                                  swipe = true;
                                  counter.setText(Integer.toString(count));
                              }
  
                          } catch (Exception e) {
                              // nothing
                          }
                          return super.onFling(e1, e2, velocityX, velocityY);
                      }
                  });
  
          view.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  return gesture.onTouchEvent(event);
              }
          });
  
  
          return view;
      }
  
      @Override
      public void onClick(View v) {
  
           if (swipe) {
               counter.setText("0");
               swipe = false;
           } else {
               count++;
               counter.setText(Integer.toString(count));
           }
       }
  
  
      @Override
      public boolean onDown(MotionEvent e) {
          return false;
      }
  
      @Override
      public void onShowPress(MotionEvent e) {
  
      }
  
      @Override
      public boolean onSingleTapUp(MotionEvent e) {
          return false;
      }
  
      @Override
      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
          return false;
      }
  
      @Override
      public void onLongPress(MotionEvent e) {
  
      }
  
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
          return false;
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>These are the important bits that made the solution work for my code:</p>
  
  <p>The idea was: FragmentCounter increases counter --> sets value in MainActivity --> MainActivity calls updateCall method in FragmentCalculations --> FragmentCalculations updates textview.</p>
  
  <p>Main Activity:</p>
  
  <pre><code>private FragmentCalculations fragcalc;
  
      // get latest count from FragmentCounter and call updateCall method in FragmentCalculations
      public void setLastCount(int count) {
          ((FragmentCalculations)fragcalc).updateCall(count);
      }
  </code></pre>
  
  <p>FragmentCounter:
  This code goes into the onclicklistener of the button which I use to increment my counter:</p>
  
  <pre><code>((MainActivity)getActivity()).setLastCount(count);
  </code></pre>
  
  <p>FragmentCalculations:</p>
  
  <pre><code>public class FragmentCalculations extends Fragment{
  
  public static EditText numberOfSquaresRef, countedCellsRef;
  
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.fragment_calculation, container, false);
  
      countedCellsRef = (EditText) view.findViewById(R.id.counted_cells);
  
   public void updateCall(int count){
      Log.d(TAG, "fragcalc value: " + count);
      countedCellsRef.setText(""+count);
  }
  </code></pre>