Why do I need setRetainInstance or onSaveInstance if I can use android: configChanges = "keyboard | orientation | screenLayout"

Why do I need to use setRetainInstance() or onSaveInstance() to save state, and I can use android:configChanges="keyboard|orientation|screenLayout" and get "state state without user state state"? I mean with less headache.

+4
source share
1 answer

Do not use android:configChanges . This will break things in a subtle way and prevent Android from getting the correct layout / theme / size, etc. For current configuration.

onSaveInstanceState() completely orthogonal to this: you need to save state in order to restore it if Android kills your process to save memory. configChagnes only prevents the re-creation of activity during rotation, changing the state of the keyboard, etc.

setRetainInstance() is for fragments that you don’t want to re-create on device rotation, etc. If you do not, Android will serialize its state in the Bundle and recreate them along with the parent Activities.

In short, while configChanges seems like a "shortcut", it is not. Do not rely on it and do not save / restore the state as needed, using the appropriate tools for each case.

+7
source

All Articles