1

I have a survey app which is implemented using a viewpager and an arraylist of fragments. The fragments have different view types, some have radio buttons, some input boxes. I want the save the entered data to a shared variable in the parent activity when the user navigates from one fragment to another. When the user reaches the last fragment i want to display the summary of the data. i was thinking of saving the data when the user navigates from one fragment to the next. Also not sure if it is the best way to go about it.

    List<Question> questions = new SurveyServiceImpl(getApplicationContext()).getSurveyQuestions(1);
    ArrayList<Fragment> questionFragments = new ArrayList<>();
    questionFragments.add(HomeFragment.newInstance("", ""));
    for (int i = 0; i < questions.size(); i++) {
        switch (questions.get(i).getQuestionType()) {
            case SELECT:
                if (questions.get(i).getMaximumOptionsRequired() == 1)
                    questionFragments.add(QuestionTypeSelect.newInstance("", questions.get(i)));
                else
                    questionFragments.add(QuestionTypeCheckBox.newInstance("", questions.get(i)));
                break;
            case INPUT:
                questionFragments.add(QuestionTypeInput.newInstance("", questions.get(i)));
                break;
            default:
        }
    }
    questionFragments.add(EndFragment.newInstance("", ""));
    final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), questionFragments));
Hopewell Mutanda
  • 1,035
  • 3
  • 11
  • 18

3 Answers3

1

Use OnPageChangeListener for: all scrolls; page centered; page scroll changes (start drag, finish center animation)

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
        @Override public void onPageSelected(int position) { }
        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_DRAGGING) {
//User started moving current page
}});
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

Off the top of my head: Save the data as soon as it is changed in each fragment :
For EditTexts, use TextWatcher : (example) to save the text when it changes in afterTextChanged().
For RadioButtons, use radioButton.setOnClickListener(/**Save data in onClick*/), or radioGroup.setOnCheckedChangeListener if you're using a RadioGroup (example).
Might not be the most efficient solution, but works :) .

Neeraj
  • 2,376
  • 2
  • 24
  • 41
0

Hmm. Well unless you are doing some sort of save checkmarks along the wizard flow that you want to represent that all data for that page was saved, I would not go that route personally. You can easily just recognize pageChanged listener

      vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //Generated method stub
        }

        @Override
        public void onPageSelected(int position) {
            setCurrentPage(position);
            PAGE_TO_RETURN_TO = position;
            vpMain.setCurrentItem(PAGE_TO_RETURN_TO);
            validate data and allow to scroll or save
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            //Generated Method Stub
        }
    });

but I would prefer to see onPause() of the parent activity handle saving the data personally. But it really depends on your need for the app.

Sam
  • 5,342
  • 1
  • 23
  • 39