1

I have a PagerFragmentAdapter with two tabs but only one fragment PartOne() :

@Override
    public Fragment getItem(int arg0) {
        Bundle data = new Bundle();
        switch(arg0){
            case 0:
                PartOne oneFragment = new PartOne();                
                data.putInt("current_page", arg0+1);
                oneFragment.setArguments(data);
                return oneFragment;
            case 1:
                PartOne twoFragment = new PartOne();
                data.putInt("current_page", arg0+1);
                twoFragment.setArguments(data);
                return twoFragment; 
        }
        return null;
    }

When PartOne() loads, I retrieve

partChosen = getArguments().getInt("current_page");

that I use

if (partChosen == 1) { // Code }

Used as described, the two instances of the fragment act the same way, giving the same results(as if partChosen == 2 even if it's not). However, when I replace the Fragment PartOne() by PartTwo()* in case 1, they act the way I need them to.

*PartTwo() has the exact same code as PartOne(), just a different name.

Please, what am I doing wrong?

4
  • Is partChosen static? Mar 15, 2013 at 15:23
  • Waouuu you found it thanx Benito !
    – 2t0m
    Mar 15, 2013 at 15:32
  • Could you please explain me why it got stuck? And add an answer that I can validate !
    – 2t0m
    Mar 15, 2013 at 15:34
  • static variables are shared between all instances of the same class. Mar 15, 2013 at 15:39

2 Answers 2

0

Aside from repetition, your code is fine.

A better way to write getItem() would be:

@Override
public Fragment getItem(int position) {
    Bundle data = new Bundle();
    PartOne oneFragment = new PartOne();                
    data.putInt("current_page", position + 1);
    oneFragment.setArguments(data);
    return oneFragment;

}

The only problem I can think of is you declared partChosen static.

0

I had the same issue and resolved with:

@Override
    public Fragment getItem(int arg0) {

        switch(arg0){
            case 0:
                Bundle data = new Bundle();
                PartOne oneFragment = new PartOne();                
                data.putInt("current_page", arg0+1);
                oneFragment.setArguments(data);
                return oneFragment;
            case 1:
                Bundle data = new Bundle();
                PartOne twoFragment = new PartOne();
                data.putInt("current_page", arg0+1);
                twoFragment.setArguments(data);
                return twoFragment; 
        }
        return null;
    }

and get the args in the onCreateView method of the fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_monday, container, false);
        TextView textView = (TextView) view.findViewById(R.id.textview1);
        textView.setText(getArguments().getInt("current_page"););

        return view;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.