0

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?

Anil kumar
  • 1,534
  • 3
  • 14
  • 20
  • 2
    you can set onConfigchanges attribute in mainfest.xml to stop activity from reloading – Biraj Zalavadia Nov 18 '13 at 09:55
  • Why is 20 fields any different from 2 fields? What I do is store my UI data in a container object and then I pass that container to onSaveInstanceState(). I need to handle only one direct object reference no matter how much data I have in the container. – Torben Nov 18 '13 at 09:56
  • This link will help you. http://stackoverflow.com/questions/12049519/edittext-data-is-lost-on-rotating-the-device – Mani Nov 18 '13 at 09:57
  • @Torben getting & setting 20 fields wont be heavy for thread? Now I have only 20, but think if you have hundreds of fields. – Anil kumar Nov 18 '13 at 10:06
  • 20 fields is nothing at all. You shouldn't worry about it. – Ricardo Nov 18 '13 at 10:11
  • @Ricardo - ya I agree with you but I posted to know better approach other than OnSavedInstance & modelObject – Anil kumar Nov 18 '13 at 10:20

3 Answers3

1

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 .

Community
  • 1
  • 1
zohreh
  • 1,055
  • 1
  • 9
  • 26
  • read http://developer.android.com/guide/topics/manifest/activity-element.html#config.that help you – zohreh Nov 18 '13 at 10:04
  • Thanks Zohreh http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation link is very good – Anil kumar Nov 18 '13 at 10:24
1

Try this way

<activity
    android:name=".ActivityName"
    android:configChanges="orientation|screenSize|keyboardHidden"/>
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
1

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.

Community
  • 1
  • 1
Ricardo
  • 7,785
  • 8
  • 40
  • 60