Pagine

Thursday, October 9, 2014

Automate Save State Feature in Firemonkey

Hi!
One of the many exciting new features of Delphi XE7 is the Save State in Firemonkey.
The FireMonkey Save State feature allows you to save data describing the state of the application before it closes. The Save State data can be used to recover the state after the application is restarted.
This is a very cool feature but the way in which you save and restore the data is not so refined (for more info see this http://docwiki.embarcadero.com/RADStudio/XE7/en/FireMonkey_Save_State ).
What happen if I need to save 10,15 or more components? I need to specify the save and the restore for each one.
So I decided to write a class helper to automate this process!
Using JSON serialization all the components of type TFMXObject are saved. In particular is stored the Data property.

7 comments
:

which version of Android tested? It seems not working well on Android 5.0 and up. I have to investigate, but probably has changed something in the app-lifecycle because the FormSaveState event is not fired anymore when app shut-down.

Doesn't work also for me.Delphi XE8 on Android 4 and also on android 5.OnSaveState don't fire. I put the procedure in OnFormClose so now it fires, but anyway i found NO data reading the State (both leaving empty the Path and name and valorizing them.Compiling in Windows it works fine.

It works great on Android. If you kill the Android app then it doesn't persist the data unless you specify a storage path. You have to wait for the activity to get pushed out of memory.

The easiest way to test it is to go into Developer Options and enable "Don't keep activities" (toward the bottom, under Apps). That will cause the activities to get removed automatically as soon as you switch out of them.