title: How pass fragment arguments while using a view pager with different fragments/layouts?
body: <p><strong>Objective</strong>: To use fragment arguments to pass along the string value from a <code>TextView</code> to a new fragments <code>TextView</code>, BUT while using a <code>ViewPager</code> with different <code>layouts/fragments</code> in the <code>FragmentPagerAdapter</code>.</p>
  
  <p><strong>Problem</strong>: The new fragment never receives the fragments arguments from the previous fragment. </p>
  
  <p><strong>My Set up</strong>: I have my <code>Activity</code> hosting the <code>ViewPager</code> and <code>FragmentPagerAdapter</code>. I have overridden <code>FragmentPagerAdapters</code> <code>getItem(int position)</code> method to create a new <code>fragment</code> instance depending on the current position of the <code>ViewPager</code>. Right now I only have two <code>Fragments</code> in my adapter, but will be adding more after getting over this obstacle. I am using the <code>newInstance()</code> technique to pass along the <code>Fragment</code>'s arguments, but nothing is ever passed.</p>
  
  <p><strong>Pertinent Code</strong>:</p>
  
  <p>My FragmentPagerAdapter code:</p>
  
  <pre><code>//this is a variable that I pass in as the newInstanct() methods parameter,
  // I update   this variable from my fragments constantly
  public static String fragmentArgumentsValue = "";
  
  mViewPager.setAdapter(new FragmentPagerAdapter(fm) {
  
  
              @Override
              public int getCount() {
  
                  return NUMBER_OF_VIEWS;
              }
  
              @Override
              public Fragment getItem(int position) {
                  switch (position) {
                  case 0:
                      Log.d(TAG, "---In getPosition(), position 0---");
  
                      return Frag1
                              .newInstance(fragmentArgumentsValue);
  
                  case 1:
                      Log.d(TAG, "---In getPosition(), position 1---");
  
                      return frag2
                              .newInstance(fragmentArgumentsValue);
  
                  default:
                      Log.d(TAG, "---In getPosition(), DEFAULT---");
  
                      return frag1
                              .newInstance(fragmentArgumentsValue);
                  }
  
              }
          });
  </code></pre>
  
  <p>One of the fragments newInstance() method:</p>
  
  <pre><code>public static Fragment newInstance(String fragmentArgumentsValue) {
      Frag1 f = new Frag1();
      Bundle bun = new Bundle();
      bun.putString(KEY_FRAGMENT_ARGUMENTS_STRING, fragmentArgumentsValue);
      f.setArguments(bun);
      return f;
  }
  </code></pre>
  
  <p>Getting the fragments arguments:</p>
  
  <pre><code>String argString = getArguments().getString(
          KEY_FRAGMENT_ARGUMENTS_STRING);
  if (argString.length() != 0) {
      Log.d(TAG, "Trying to set the frag args to:" + argString);
      mWorkingTextView.setText("" + argString);
  } else {
      Log.d(TAG, "Couldn't set frag args to: " + argString);
  }
  </code></pre>
  
  <p><strong>What I've Tried</strong>: I've tried giving the <code>Activity</code> that hosts the <code>ViewPager</code> and <code>FragmentPagerAdapter</code> a <code>static</code> variable that I constantly update in each one of my <code>fragments</code>, I include the <code>static</code> variable in the <code>fragment.newInstance(staticVariableInActivity)</code> method, but this doesn't seem to work.
  I've also tried using the <code>ViewPager</code> callback <code>viewPager.setOnPageChangeListener()</code> I had overridden the <code>onPageSelected(int pos)</code> and tried to pass the fragment arguments there, nevertheless it didn't work... so please help me S.O.!!!</p>
  
  <p><strong>My thoughts</strong>: I do not have the different <code>fragments</code> and <code>layouts</code> in an <code>ArrayList</code> or any list for that matter, I just instantiate the <code>Fragments</code> via the <code>newInstance()</code> method depending on the positions of the <code>FragementPagerAdapter</code>, could this be a problem? Should I create a <code>singleton</code> list of the <code>layouts/fragments</code>? So I can change the values of the <code>Fragments</code> <code>TextViews</code> via the <code>singleton</code> list? (excuse me if that's a dumb or not possible thing to do).</p>
  
  <p><strong>Note</strong>: I am am saving away the TextViews values via <code>public void onSaveInstanceState(Bundle outState)</code> to handle screen rotations. Could this be a problem? </p>