Search code examples
androidandroid-dialogfragment

Retain DialogFragment state on orientation change


I have a DialogFragment which loads info from a file and creates a certain number of EditText fields on the screen (there can be 5-6 minimum, or there can be dozens of them). They all have an initial value when loaded form the file.
The user can also add additional text fields by pressing a button. These text fields can be added in multiple places, in between existing fields. Everything is added programmatically.
When an orientation change occurs, all this is lost. Is there a way to prevent this from happening? Not by saving info and then restoring it, but simply to prevent it from happening at all?

The number of fields at dialog creation depends on the information that will be loaded from the file. The number of fields when an orientation change happens does not have to be the same. New fields could have been added by the user pressing one out of multiple buttons that adds new fields. The dialog can have 30+ fields at one point. Saving state and then restoring it could require a complex method (probably something like saving the views in a static list, or maybe the entire layout). Preventing orientation change entirely while the dialog is open is probably the best option, or maybe forcing it to go landscape since the user will need to type a lot...

Is there a simpler way to solve this?


Solution

  • Yes, you can add attribute android:configChanges="orientation|screenSize|keyboard|keyboardHidden" to the Activity declaration in the AndroidManifest.xml file.