Nikola Srdoč
Nikola Srdoč

Reputation: 321

Sending list of fragments to FragmentPagerAdapter

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?

Upvotes: 1

Views: 205

Answers (1)

Alex Townsend
Alex Townsend

Reputation: 1564

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.

Upvotes: 1

Related Questions