0

In my FragmentActvitiy I am using a ViewPager, which I fill by using FragmentStatePagerAdapter.

When I try to access the fragment views, with intention to get / update visual elements I get a nullpointer exception. I can access all the fragments and their arguments, but their views return null.

MyPageAdapter adapter = (MyPageAdapter) pager.getAdapter();
Fragment fragment;

for (int i=0; i<15; i++) {
    fragment = adapter.getItem(i);
    View v = fragment.getView();
    System.out.println("--- FRAGMENT = " + fragment + "   VIEW = " + v);
}

I am aware that FragmentStatePagerAdapter deletes views to manage memory resources, and that I shouldn't access the views inside my FragmentActivity.

My question is what is the appropriate way of storing / manipulating visual elements of the fragments?

Should I implement OnPageChangeListener in my ViewPager class and somehow store the fragments values to SharedPreferences and than recreate them - Where?

Marko
  • 20,385
  • 13
  • 48
  • 64

1 Answers1

1

Your fragments should really manage their own views. What you could do is add methods to your fragments that will alert those fragments that they need to update their views. Call those methods from the FragmentStatePagerAdapter or your hosting Activity. Store the desired changes in the Fragment, and then in onCreateview(...) update the views with the new data or whatever you want to change in it.

Nathan Walters
  • 4,116
  • 4
  • 23
  • 37
  • I get what you are saying. I made an method in Fragment class that returns me the selected values. My problem is that my application is a Quiz and thats why I would like to store values from all the fragments. How an,d where could I iterate/ store the fragments values? – Marko Jan 03 '14 at 18:37
  • Are you trying to retrieve values from the fragments or change their views? Please be specific with what you want to do. If it is a quiz, you probably want to prevent people from moving back to previous questions, which would simplify things a bit. Please describe your scenario in a bit more detail. – Nathan Walters Jan 03 '14 at 18:49
  • I have a quiz of 15 questions, when the user clicks on button Finish I want to retrieve the values from the fragments/questions and evaluate the answers (the selected answer - already implemented in Fragment class). I want the user to move freely back and forth in case they want to look through or change the answer. Any idea what I could try? – Marko Jan 03 '14 at 18:58
  • You could store a `Bundle` object in your hosting `Activity`, and have a getter method for it. In the fragments, override `onPause(...)`, retrieve the `Bundle` from the hosting `Activity`, and store the current quiz answer in the `Bundle`, either as a `boolean` right/wrong answer, `String` a/b/c/d answer, or `int` index of the selected answer; this will depend on how you check right/wrong answers. Once they click finish, check their selected answers now stored in the `Bundle` against the correct answers and give them a score. – Nathan Walters Jan 03 '14 at 19:19
  • This is great. I followed what you said, but the onPause() gets called only when the fragments gets "destroyed" so when it is two swipes away, so for 1st question, it gets called on 3rd question. But your tips got me in the right way so I implemented OnCheckedChangeListener in my Fragment class, so I detect the answers beeing selected right away and store them in a Bundle. Thanks for your help! ;) – Marko Jan 03 '14 at 23:16