I don't know if I get your problem correctly and I don't know if you have 100 fragments, if so, of course it can lead to memory issues and performance problems due the amount of Fragments created.
What I recommend is to have just one Fragment for the survey and dynamically populate the questions based on a data source, JSON, whatever and inside that Fragment have a ViewPager or RecyclerView.
Pseudo code
In the Activity create the instance of your ViewPager
ViewPager viewPager = findViewById(R.id.viewPager);
QuestionPagerAdapter adapter = new QuestionPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
Then your QuestionPagerAdapter should be like this
@Override
public Fragment getItem(int position) {
QuestionFragment fragment = new QuestionFragment();
fragment.setQuestion(questionList.get(position));
return fragment;
}
@Override
public int getCount() {
return questionList.size(); //Assuming you have a list of questions
}
Then in your single Fragment have a method of setQuestion
```
public void setQuestion(Question question) {
this.question = question;
}
```
Use bundle to pass data to the Fragment
And in the onCreateView do the logic to show the Question and move to the next question
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_question, container, false);
logicToPrintQuestion();
//logic to change the question
nextButton.setOnClickListener(....)
//Here add the answer to a list if you want
//Using the viewPager to change the
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
return view;
}
This may not compile but it's an idea of how would be one possible implementation.
Edit
You are right, ViewPager you can swipe between them, what you can do is create a custom ViewPager as :
class NonSwipeableViewPager(context: Context, attrs: AttributeSet?) : ViewPager(context, attrs) {
override fun onTouchEvent(event: MotionEvent): Boolean {
return false
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
return false
}
}
So in your xml should be :
<com.yourpackage.appname.NonSwipeableViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />