title: Get Fragment by Tag
body: <p>I am using a <strong>FragmentPagerAdapter</strong> to create a multi-pages UI with different fragments. What I want to do is to find the Fragment using the position of the Fragment. </p>
  
  <p>I have applied the method of finding the nametag of fragments and use .FindFragmentbyTag() but the .FindFragmentbyTag always return me null value. The structure seems to be correct so I am not sure why I get null values, please look through and help is appreciated. </p>
  
  <p>Here is the code</p>
  
  <pre><code>MainActivity.java
  public void onCreate(Bundle savedInstanceState) {
      this.getSupportFragmentManager();
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      MainActivity.instance=this;
      //Initialize pager
      vpPager = (ViewPager)findViewById(R.id.mypager);
      pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
      vpPager.setAdapter(pagerAdapter);
      this.mapFragment = (MapFragment)this.findFragmentByPosition(0);
      this.drFragment = (DRFragment)this.findFragmentByPosition(1);
      this.sensorFragment = (SensorFragment)this.findFragmentByPosition(2);
      ......
  }
  
  protected Fragment  findFragmentByPosition(int position) {
      int pagerid = this.vpPager.getId();
      long pagerAdapterid = this.pagerAdapter.getItemId(position);
      return getSupportFragmentManager().findFragmentByTag("android:switcher:" + pagerid + ":" + pagerAdapterid + "");
  }
  </code></pre>
  
  <hr>
  
  <pre><code>MyPagerAdapter.java
  import android.os.Parcelable;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.view.View;
  
  public class MyPagerAdapter extends FragmentPagerAdapter {
      private static final String TAG = "TM_PageAdapter";
      private static int numItems = 3;
  
      public MyPagerAdapter(FragmentManager fragmentManager){
          super(fragmentManager);
      }
  
      public int getCount() {
          return numItems;
      }
  
      @Override
      public Fragment getItem(int position) {
          switch(position){
              case 0:
                  return MapFragment.newInstance(0, "Map Information");
              case 1:
                  return DRFragment.newInstance(1, "DR Information");
              case 2: 
                  return SensorFragment.newInstance(2, "Sensor Information");
              default: 
                  return null;
          }
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch(position){
              case 0:
                  return "Map Information";
              case 1:
                  return "DR Information";
              case 2: 
                  return "Sensor Information";
              default:
                  return null;
          }
      }
  
      public static String makeFragmentName(int viewId, int index) {
          return "android:switcher:" + viewId + ":" + index;
     }
  
  
      @Override
      public boolean isViewFromObject(View arg0, Object arg1) {
          return arg0 == ((View) arg1);
  
      }
  
      @Override
      public Parcelable saveState() {
          return null;
      }
  </code></pre>
  
  <p>Here is one of the code of the .newInstance: </p>
  
  <pre><code>public static MapFragment newInstance(int position, String title){
      MapFragment mapFragment = new MapFragment();
      Bundle args = new Bundle();
      args.putInt("current_page", 0);
      args.putString("page_tile", "Map Information");
      mapFragment.setArguments(args);
      return mapFragment;
  }
  </code></pre>