title: How to shift between two fragments activities each other in android
body: <p>I have two fragment activities . Now I want to shift between this .Also I want to do this after end of the first fragment activity.</p>
  
  <p>Here are my fragment activity classes,</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.os.Bundle;
  import android.view.MotionEvent;
  import android.view.View;
  
  public class MainActivity extends FragmentActivity {
  
      private ViewPager pager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final int page=2;
  
          pager = (ViewPager) findViewById(R.id.viewPager);
  
          pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
          pager.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  if (pager.getCurrentItem()==page) {
                      return  true;
                  }
                  return false;
              }
          });
  
      }
  
      private class MyPagerAdapter extends FragmentPagerAdapter {
  
          public MyPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int pos) {
              switch(pos) {
  
                  case 0: return FirstFragment.newInstance("FirstFragment");
                  case 1: return SecondFragment.newInstance("SecondFragment");
                  case 2: return ThirdFragment.newInstance("ThirdFragment");
                  case 3: return FourthFragment.newInstance("FourthFragment");
                  case 4: return FifthFragment.newInstance("FifthFragment");
                  default: return ThirdFragment.newInstance("DefaultFragment");
              }
          }
  
          @Override
          public int getCount() {
              return 5;
          }
  
  
  
      }
  
      public void setCurrentItem(int which) {
          if(pager != null &amp;&amp; which &gt;= 0 &amp;&amp; which &lt;= 4) {
              pager.setCurrentItem(which);
          }
      }
  }
  </code></pre>
  
  <p><strong>SecondActivity.java</strong></p>
  
  <pre><code>import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.os.Bundle;
  import android.view.MotionEvent;
  import android.view.View;
  
  public class SecondActivity extends FragmentActivity {
  
      private ViewPager pager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);
  
          final int page=2;
  
          pager = (ViewPager) findViewById(R.id.viewPager_1);
  
          pager.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
          pager.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  if (pager.getCurrentItem()==page) {
                      return  true;
                  }
                  return false;
              }
          });
  
      }
  
      private class MyPagerAdapter2 extends FragmentPagerAdapter {
  
          public MyPagerAdapter2(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int pos) {
              switch(pos) {
  
                  case 0: return FirstFragment_1.newInstance("FirstFragment_2");
                  case 1: return SecondFragment_1.newInstance("SecondFragment_2");
                  case 2: return ThirdFragment_1.newInstance("ThirdFragment_2");
                  case 3: return FourthFragment_1.newInstance("FourthFragment_2");
                  case 4: return FifthFragment_1.newInstance("FifthFragment_2");
                  default: return ThirdFragment_1.newInstance("DefaultFragment_2");
              }
          }
  
          @Override
          public int getCount() {
              return 5;
          }
  
  
  
      }
  
      public void setCurrentItem(int which) {
          if(pager != null &amp;&amp; which &gt;= 0 &amp;&amp; which &lt;= 4) {
              pager.setCurrentItem(which);
          }
      }
  }
  </code></pre>
  
  <p><strong>Problem</strong> How could I do this?</p>