-2

I have an Activity which uses several Fragments. One of my fragments have an ExpandableListView which uses an Adapter of self made. What is the good strategy to keep(or should I keep) and recreate/retain when screen orientation changes:

  1. reference of the Fragment in Activity
  2. ExpandableListView in the fragment
  3. ExpandableListView's states (such as collapsed/opened groups)

What I am currently doing is: reCreating the Fragment,Adapter, ExpandableListView inside Fragment each and everytime. (Not keeping states yet, but I guess I will use SharedPreferences).

PS: to minus voters: please elaborate what I am doing wrong. Apparently I am missing some points.

guness
  • 6,336
  • 7
  • 59
  • 88
  • You should not to use `SharedPreferences` to do this. – Blo Dec 10 '13 at 17:32
  • 1
    Instead try `putFragment` and `getFragment/restoreFragment` see my answer here: http://stackoverflow.com/questions/20189151/handle-fragment-on-screen-orientation-changes/20189415#20189415 – Blo Dec 10 '13 at 17:43

1 Answers1

0

If you don't have nested fragments, you can use setRetainInstance(true). Or you can override onSaveInstanceState(Bundle) and save all your data, that you'll repopulate in onCreate / onCreateView. Note that the data has to be Serializable or Parcelable (preferred).

I think the downvote is because this question has been asked 10000 times :)

mbmc
  • 5,024
  • 5
  • 25
  • 53