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));