I've been programming for Android for some time, and I'm still looking for solutions to retain data over configuration changes. Aside from saving Parcelable
s to Activity's Bundle
in onSaveInstanceState
docs are suggesting using Fragment
with setRetainInstance
flag set to true.
But I've just come across some code that uses onRetainCustomNonConfigurationInstance
to hold arbitrary objects (in a fancy way, but essentially big objects without references to Activity
etc.). I have never seen this method used, so I have some doubts:
- Is this method safe to call to store arbitrary objects (in a sense that I can be pretty sure it's gonna get called, and that it won't be deprecated/removed anytime soon)?
- How is this method different from
onRetainNonConfigurationInstance()
, which also should returnObject
, and in essence should work similarly? - Is using retained fragment still better, for some reason?
As a bonus, I would be grateful for any other tips or solutions to save state of objects like AsyncTask
, Observable
, view's presenters and go on