Reputation: 1534
I have one activity which has more than 20 fields, most of these fields will prompt user to set data.
Now the problem with the Configuration changes. If user set all the fields & he/she changed screen orientation, then all the fields will be reset because the activity will restart.
If I have only few fields means, then I would have gone with onSaveInstanceState() & onRestoreInstanceState().
But how to handle these many fields? Whether I have to go by storing all the fields? or Is there any better approach?
Upvotes: 0
Views: 59
Reputation: 1045
add android:configChanges="keyboardHidden|orientation|screenSize"
to activity tag in manifest file but that is not best way please read Why not use always android:configChanges="keyboardHidden|orientation"? and http://developer.android.com/guide/topics/manifest/activity-element.html#config and http://developer.android.com/guide/topics/resources/runtime-changes.html for selecting best way for yourself here is a good example for using from parcle .
Upvotes: 1
Reputation: 7975
While you can prevent the activity from rotating to avoid the problem, this should only be done in special circumstances. Also, there are other situations where configuration changes can take place and cause problems. For a full discussion, see this: Why not use always android:configChanges="keyboardHidden|orientation"?
So my suggestion is that instead you create a holder class that implements Parcelable. Then on onSaveInstanceState(Bundle outState)
simply call outState.putParcelable(key, yourParcelableObject)
. You would still need to update the corresponding fields on and this class but at least you would avoid having several keys and calling individual put
to the bundle.
Upvotes: 1
Reputation: 28484
Try this way
<activity
android:name=".ActivityName"
android:configChanges="orientation|screenSize|keyboardHidden"/>
Upvotes: 1