How do I pass a variable through a FragmentPagerAdapter to a Fragment?

David Walton picture David Walton · Feb 19, 2013 · Viewed 19.7k times · Source

I'm an android beginner, trying to learn, and this is my first question, so please excuse me if the question is too simple and please tell me if I'm using the forum incorrectly.

I have a FragmentActivity using a layout that includes a viewpager; the activity creates an instance of MyFragmentPagerAdapter, which creates several instances of MyFragment. All are support.v4 versions.

I'm trying to find a way to pass an integer value (representing a selection the user has made previously, which could actually be a drawable id) to the fragment. I'm using an intent to pass it to this activity and I know I can can use a bundle to pass a value from the FragmentPagerAdapter to the fragment, but I can't find a way to get it from the activity to the FragmentPagerAdapter. I have tried several ways including changing the constructor but couldn't get that to work.

Am I missing something simple? What is the best way to do this?

Activity

    public class SecondActivity extends FragmentActivity {
      private MyIntegerAdapter1 mAdapter1;
      private ViewPager mPager1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        mAdapter1 = new MyIntegerAdapter1(getSupportFragmentManager());
        mPager1 = (ViewPager) findViewById(R.id.pager1);
        mPager1.setAdapter(mAdapter1);

FragmentPagerAdapter

    public class MyIntegerAdapter1 extends FragmentPagerAdapter {
    public MyIntegerAdapter1(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            Fragment f1 = new IntegerFragment();
            Bundle args1 = new Bundle();
            args1.putInt("param", R.drawable.image1);
            args1.putInt("number", 1);
            f1.setArguments(args1);
            return f1;
    etc

Fragment

    public class IntegerFragment extends Fragment {
      private int imageResourceId;
      private int numberSelected;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //change to avoid orientation crash
        imageResourceId = getArguments().getInt("param");
        numberSelected = getArguments().getInt("number");

Answer

moskis picture moskis · Feb 19, 2013

I extract this from the original Google Samples.

Fragment:

public class IntegerFragment extends Fragment {
    int imageResourceId;
    int numberSelected;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //change to avoid orientation crash
        imageResourceId = getArguments().getInt("param");
        numberSelected = getArguments().getInt("number");
    }
    
    public static Fragment newInstance(int imageResourceId, int numberSelected) {
        IntegerFragment f = new IntegerFragment();
        f.imageResourceId = imageResourceId;
        f.numberSelected = numberSelected;
        return f;
    }

FragmentPagerAdapter

public class MyIntegerAdapter1 extends FragmentPagerAdapter {
    private Fragment[] fragments;

    public MyIntegerAdapter1(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
        if (content != null && position < content.length) {
        if (fragments[position] == null) {
            fragments[position] = IntegerFragment.newInstance(R.drawable.image1, 1);
        }
        return fragments[position % fragments.length];
    }
    return null;
etc