0

I'm developing an android java app that is like a survey with more than 100 questions. The user can see each question at a time so for each question I make a fragment that pass one by one, let´s call it QuestionFragment Sometimes when user is around the 50th question the app chrashes and restore with problems. Seems to be a memory problem. The way I'm creating fragments is this code:

QuestionFragment nvofrag = new QuestionFragment(nvoReac,false);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.continf_fragment, nvofrag);
fragmentTransaction.commit();

This code is inside QuestionFargment and it is called in the "next question" button so the question is Am I creating the fragments properly? Is my programming logic correct? I hope to be clear. Any idea or comment how to google my problem could be for help.

  • So you have 100 Fragment class??? – Skizo-ozᴉʞS ツ May 11 '23 at 06:34
  • Hi Skizo Yes, I think so. What I wanted to do is to create a totally new fragment when the users push "next question" button and destroy previous fragment. – Marisol patito May 11 '23 at 06:44
  • So...100 Fragments is a lot, but...with replace, it shouldn't be enough to crash an app, unless you've got something else causing a memory leak. What is the crash message in logcat? – Ryan M May 12 '23 at 03:47
  • Also [don't pass arguments in the constructor like that](https://stackoverflow.com/q/65514124/208273). But that's not your problem here. – Ryan M May 12 '23 at 03:47
  • @Ryan M There's no crash message, but sometimes the app closes and restore, so I think is about memory leak. I think as fragments passes one to another, previous is destroyed – Marisol patito May 12 '23 at 05:30
  • See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for advice on how to get the crash message. – Ryan M May 12 '23 at 10:18
  • (in other words: if it *is* a memory leak, there should be a crash message in the logs) – Ryan M May 16 '23 at 07:17

1 Answers1

0

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" />

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Thank you so much for your intresting and help. As you can see I'm not expirienced in android developer, I think viewpager creates kind of tabs, but what i need is the user must answer one question to pass next and user just see one question at a time – Marisol patito May 11 '23 at 07:02
  • 1
    As I can understand you, that custom viewpager could help with questions transition and the ui layout in one fragment? If so let me try it. – Marisol patito May 11 '23 at 07:34
  • The viewpager won't let you move through the questions manually, it only will work when you programmatically change the question, so I guess is what you are looking for. – Skizo-ozᴉʞS ツ May 11 '23 at 07:37
  • Yes, you've go it perfect. I've asked that because my main problem at first was how to make the view dinamically because the questions sometimes needs spinners, another times edittext, another to take a photo and so on – Marisol patito May 11 '23 at 07:46
  • Excuse me, would you tell me please if with the adapter solution there is not creating 100 items or that class is like recyclerview which manages the creation? – Marisol patito May 11 '23 at 08:22
  • There's something in your Fragment should have the Logic to show the desired View, or if you know that there are just 3/4 types, you can create the ViewPager with the Fragment you want. – Skizo-ozᴉʞS ツ May 11 '23 at 08:53
  • You can add `RecyclerView` to a `ViewPager` here https://developersbreach.com/recyclerview-to-viewpager2-materialviewpager-android/ – Skizo-ozᴉʞS ツ May 11 '23 at 08:54