title: getSupportFragmentManager getFragment without tag or id
body: <p>I'm developing an Android 3.1 Tablet application.</p>
  
  <p>I'm using ViewPager and fragments on one activity. My problem is that I don't know how to set an ID or Tag to fragments.</p>
  
  <p><strong>Activity</strong></p>
  
  <pre><code>public class FillEReportFragmentActivity extends FragmentActivity
  {
      ...
  
      /**
       * View Pager to manage form parts.
       */
      private ViewPager mViewPager;
      /**
       * Page Adapter to handle fragments.
       */
      private FillEReportFragmentPagerAdapter mPagerAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstance)
      {
          Log.v("FillEReportFragmentActivity", "onCreate");
  
          super.onCreate(savedInstance);
          setContentView(R.layout.fill_ereport_paged);
  
          // Recoge los argumentos que le llegan de la actividad padre.
          Bundle extras = getIntent().getExtras();
          if (extras != null)
          {
              qapId = extras.getLong(BundleKeys.qapIdKey);
              orderId = extras.getLong(BundleKeys.orderIdKey);
              eReportId = extras.getLong(BundleKeys.eReportIdKey);
          }
  
          // Cargamos el E-Report o creamos uno nuevo. Además se recuperan los
          // artículos de la orden actual.
          LoadEReportAsyncTask task = new LoadEReportAsyncTask(this);
          task.execute(Long.toString(this.qapId));
  
          // Establezco los fragmentos (o partes del form) que se mostrarán en el
          // View Pager.
          mViewPager = (ViewPager)findViewById(R.id.pager);
          mPagerAdapter = new FillEReportFragmentPagerAdapter(getSupportFragmentManager());
          mViewPager.setAdapter(mPagerAdapter);
  
          // Muestro en la parte inferior unos círculos para indicar donde me
          // encuentro dentro del E-Report.
          CirclePageIndicator mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
          mIndicator.setViewPager(mViewPager);
  
          // Creo unos tabs para poder navegar más facilmente a través de las
          // partes del E-Report.
          TabPageIndicator titleIndicator = (TabPageIndicator)findViewById(R.id.tabIndicator);
          titleIndicator.setViewPager(mViewPager);
      }
  
          ...
  }
  </code></pre>
  
  <p><strong>FragmentPagerAdapter</strong></p>
  
  <pre><code>public class FillEReportFragmentPagerAdapter extends FragmentPagerAdapter
  {
      private static final String[] TITLES = new String[] { 
          "Factory", 
          "Items" 
      };
  
      public FillEReportFragmentPagerAdapter(FragmentManager fm)
      {
          super(fm);
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public Fragment getItem(int index)
      {
          switch (index)
          {
              case 0:
                  return FactoryInfoFragment.newInstance();
              case 1:
                  return ItemsFragment.newInstance();
  
          default:
              return null;
          }
      }
  
      @Override
      public int getCount()
      {
          return 2; // TODO: Este número hay que cambiarlo porque serán más.
      }
  
      public String getPageTitle(int position)
      {
          return TITLES[position];
      }
  }
  </code></pre>
  
  <p><strong>res/values/ids.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;item type="id" name="frag_general_info_factory"/&gt;
      &lt;item type="id" name="frag_general_info_items"/&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p><strong>FactoryInfoFragment</strong></p>
  
  <pre><code>public class FactoryInfoFragment extends Fragment
  {
      public static FactoryInfoFragment newInstance()
      {
          FactoryInfoFragment frag = new FactoryInfoFragment();
  
          return frag;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState)
      {
          Log.v("FactoryInfoFragment", "onCreateView");
  
          View v = inflater.inflate(R.layout.frag_general_info_factory, container, false);
      view.setId(R.id.frag_general_info_factory);
  
          return v;
      }
  }
  </code></pre>
  
  <p><strong>frag_general_info_factory.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_margin="10dp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:shrinkColumns="*"  
      android:stretchColumns="*"&gt;
  
        &lt;TableRow
          android:weightSum="1"&gt; &lt;!-- 0 --&gt;
      &lt;TextView
          android:id="@+id/txtGenInfo"
          android:layout_span="8"
          android:layout_gravity="center_horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/layout_title_general_information"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
        &lt;/TableRow&gt;
  
        ...
  
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p><strong>Here I want to get an specific fragment</strong></p>
  
  <pre><code>private class LoadEReportAsyncTask extends AsyncTask&lt;String, Void, Boolean&gt;
  {
      private Context mContext;
      private ProgressDialog loadingDialog;
      private EReport eReport;
      private QAP qap;
  
      public LoadEReportAsyncTask(Context context)
      {
          Log.v("LoadEReportAsyncTask", "constructor");
          eReport = null;
          qap = null;
  
          this.mContext = context;
          loadingDialog = new ProgressDialog(mContext);
          loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          loadingDialog.setMessage(getString(R.string.msg_loading_ereport));
          loadingDialog.setCancelable(false);
          loadingDialog.show();
      }
  
      @Override
      protected Boolean doInBackground(String... params)
      {
          Log.v("LoadEReportAsyncTask", "doInBackground");
          ...
      }
  
      protected void onPostExecute(Boolean result)
      {
          Log.v("LoadEReportAsyncTask", "onPostExecute");
  
          FillEReportFragmentActivity parentActivity = (FillEReportFragmentActivity) mContext;
          if (result)
          {
              FactoryInfoFragment factFragment = (FactoryInfoFragment)getSupportFragmentManager().findFragmentById(R.id.frag_general_info_factory);
              factFragment.fillEReport(eReport, qap);
  
              Toast.makeText(getApplicationContext(), getString(R.string.msg_ereport_got_correct), Toast.LENGTH_SHORT).show();
  
              ItemsFragment fragment = (ItemsFragment)getSupportFragmentManager().findFragmentById(R.id.frag_general_info_items);
              fragment.setUpArticles(orderArticles);
          }
          else
          {
              Toast.makeText(getApplicationContext(), getString(R.string.msg_ereport_problem), Toast.LENGTH_LONG).show();
          }
  
          loadingDialog.dismiss();
      }
  }
  </code></pre>
  
  <p><strong>Here:</strong><br/><br/>
  <code>(FactoryInfoFragment)getSupportFragmentManager().findFragmentById(R.id.frag_general_info_factory);</code></p>
  
  <p>I use <code>R.id.frag_general_info_factory</code> to get that fragment reference but I get a <strong>null</strong>.</p>
  
  <p>How can I set a Tag or an Id to <code>FactoryInfoFragment</code>?</p>