1

im trying to make my FragmentPagerAdapter implementation reusable by sending list of fragments to pager adpater.

public Fragment getItem(int position) {
Fragment fragment = mFragmentList.get(position);
    if (fragment==null)
    {
      fragment=Fragment.instantiate(mContext,fragmentName????)
    }
}

Pager adapter dont uses fragment manager so tag is not defined there. Solution may be to specify fragments tag in xml but i would like to keep managing my fragments dynamically. Guess answer is in properly implementing inheritance... Any thoughts?

Nikola Srdoč
  • 311
  • 4
  • 14

1 Answers1

1

If you take a look at the documentation for Fragment.instantiate [here](http://developer.android.com/reference/android/app/Fragment.html#instantiate(android.content.Context, java.lang.String, android.os.Bundle)), you'll see that the fragment name is not the same as the fragment's tag. It's the name of the class of your fragment. So if your fragment class is MyCustomFragment, you would create it using the following:

Fragment.instantiate(mContext, MyCustomFragment.class.getName());

But, generally best practice is to use a static newInstance method for fragment creation. You can read more on that here.

Community
  • 1
  • 1
Alex Townsend
  • 1,564
  • 10
  • 13