I have an ArrayListAdapter. On configuration changes the list is not recreated. I would like to persist the list. I have used onSaveInstance() method to do this but I know it is not the best place where to store this kind of data.
A more appropriate method would be onRetainNonConfigurationInstance() but it has been deprecated in favor of setRetainInstance() in a Fragment. I don’t understand how the setRetainInstance in Fragment could replace onRetainNonConfigurationInstance for the above situation.
Thanks
If your fragment is dynamically added via a
FragmentTransaction, and you callsetRetainInstance(true)on that fragment, when the device undergoes a configuration change, Android will retain the existing fragment instance and reuse it in the newly-created activity. In all other cases, Android will discard the original fragment and create a brand-new fragment instance to go with the brand-new activity instance. If your fragment instance is retained, all of its data members are retained, so yourListViewwill be retained, along with its configuredListAdapterand everything else.So, the key question is: is the data in your ArrayList part of your data model, or not?
If it is part of your data model, you probably should be persisting it somewhere (database, JSON file, etc.), and then you can simply reload it in the brand-new fragment instance.
If it is not part of your data model, then
setRetainInstance(true), or possiblyonSaveInstanceState(), would be appropriate options.