gavin
gavin

Reputation: 325

preserving data between rotations using xml layouts

I am Wanting to preserve data when rotating from horizontal to vertical and back again; I know can be done through xml layouts by creating another layout with the same name using -land extension, (activity_main, activity_main-land).

I have no issues with this.

After rotating my device, the landscape layout comes up as expected. However, the data isn't preserved.

I have been told that hidden methods will automatically preserve the data between rotations as it will look for a landscape layout and if found, will automatically save the data. I was told that if the two layouts are identically named (activity_main, activity_main-land) the program will consider them to be the same activity and so will preserve the data.

Is there something else that I should be doing?

Thank you in advance

Upvotes: 1

Views: 42

Answers (1)

hata
hata

Reputation: 12478

hidden methods will automatically preserve the data between rotations

No, they won't. You must save and restore data approprietely.

  • Usually by overriding onSaveInstanceState you can save data with Bundle object.
  • Restoring data from Bundle object in onCreate.

Please refer Google's guide: Recreating an Activity

Upvotes: 1

Related Questions