0

I want to set visible one button in the last page but the last one element don't call the Fragment constructor. ( I debugged it )

public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        args.putParcelable(DemoObjectFragment.ARG_OBJECT,        check.getQuestionWithId(i));


        if(check.getNQuestions()==i)
            args.putBoolean(DemoObjectFragment.FINAL_QUEST,true);

@Override
    public int getCount() {

        return check.getNQuestions();
    }

And the DemoObjectFragment:

 public static class DemoObjectFragment extends Fragment {

        public static final String ARG_OBJECT = "QUESTION";
        public static final String FINAL_QUEST = "FINAL_QUEST";

        public static final int requestCode_AnswerActivity = 1;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {


            View rootView = inflater.inflate(R.layout.question_fragment, container, false);

            Bundle args = getArguments();
            final Question quest = args.getParcelable(ARG_OBJECT);

            ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                    quest.getContent());

            if(args.getBoolean(FINAL_QUEST)) {

                Button finish_test = (Button) rootView.findViewById(R.id.button_finish_test);
                finish_test.setVisibility(View.VISIBLE);
           }
        }
    }

Thanks.

Paritosh
  • 2,097
  • 3
  • 30
  • 42
user3612445
  • 145
  • 2
  • 16

1 Answers1

1

It is possible because your FragmentStatePagerAdapter reuses exsiting Fragment. Try these to make it work:

  • Set the button's visibility in the getItem() method of your adapter, not in onCreateView().

  • Add a code to make the button INVISIBLE as well -- you have to decide the button's visibility every time your Fragment is reused.

hoonj
  • 273
  • 3
  • 9
  • Well thanks for the answer. The error is silly, getNquestions return the the count questions and the getItem(Int i) starts from 0. Then the the last item is 3 and the total quest is 4. Sorry. – user3612445 Apr 27 '15 at 09:31
  • Good for you. But imo you may face some troubles when the page count increases. Use my answer if it happens. – hoonj Apr 27 '15 at 09:37