Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

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

Why should I need to use setRetainInstance() or onSaveInstance() to save state and I can use android:configChanges="keyboard|orientation|screenLayout" and get the samething "saving state non-UI state"? I mean with less headache.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't use android:configChanges. It will break things in subtle ways and will prevent Android from getting the proper layout/theme/dimensions, etc. for the current configuration.

onSaveInstanceState() is completely orthogonal to this: you need to save state so you can restore it if Android kills your process to save memory. configChagnes only prevents it from re-creating the activity on rotation, keyboard state changes, etc.

setRetainInstance() is for fragments that you don't want re-created on device rotation, etc. If you don't call it, Android will serialize their state in a Bundle, and re-create them along with the parent activity.

In short, while configChanges appears to be a 'shortcut' it is not. Don't rely on it and save/restore state as necessary using the proper tools for each case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...