1

I would like to know if there is a method to Save a Custom Class while rotating in an Android app. I want to save all instanced object called of the MatrixGame class...someone can help me?

Thanks

Altair
  • 47
  • 7

2 Answers2

1

You should serialize your object. You can implement Parcelable or implement Serializable (Parcelable is several times faster). Then you will be able to put it in a Bundle in onSaveInstanceState and restore it in onCreate or onRestoreInstanceState.

Also, you can serialize your object to String, e.g. json string. Another option would be to store your object in a database or a file. It depends on your needs.

If you don't want to recreate this object on screen rotation you can change the lifecycle of your Activity by adding a configuration change flag in AndroidManifest.xml. If we are talking about a Fragment, you can call setRetainInstance(true) to avoid fragment recreation on rotation.

Gennadii Saprykin
  • 4,305
  • 8
  • 28
  • 39
1

Since the Fragment lifecycle is independent - when you're using a Fragment you can set it so it doesn't get destroyed upon configuration changes.

As you noticed, the Activity class gets destroyed and re-created when you rotate the app (or apply other configuration changes), if you want to persist the Activity state you can use sqlite and save whatever you need in the onPause() method. Then in the onCreate() method check the DB for last known state.

If you want to avoid saving the state "forever" (meaning, the user turns off the app and tomorrow when she turns it back on - you want to start fresh and not load the last known state) you can add a timestamp and set a threshold which, if passed, the data is considered stale and gets disregarded.

Two comments:

  1. As @saprvade wrote, Fragments gets destroyed by default, in order to prevent it you should call: setRetainInstance(true)
  2. In case you want to implement @saprvade's other suggestion (have the activity ignore rotation changes) you can see the following explanation of how to implement: https://stackoverflow.com/a/456918/1057429
Community
  • 1
  • 1
Nir Alfasi
  • 49,889
  • 11
  • 75
  • 119
  • Thanks for your help. So in my case I'm using a Fragment, but once rotating the device, class instanced on the fragment is recreated...how can i solve it? – Altair Jul 07 '15 at 18:33
  • @saprvade actually covered that in his answer: by setting `setRetainInstance(true)` you're making sure the fragment won't get destroyed. – Nir Alfasi Jul 07 '15 at 18:34
  • Perfect, I've put it on the onCreate, but when rotating my custom class is redefined again! :( – Altair Jul 07 '15 at 18:36
  • @Altair remember that even though the Fragment won't get destroyed the Activity class will (and it's objects/resources will get reclaimed). So if you want to save objects that you've created within the Activity - Fragments won't help you - you'll have to use the other approach of persisting them. – Nir Alfasi Jul 07 '15 at 18:40
  • oh, perfect! Thanks for your help ;) – Altair Jul 07 '15 at 18:42