Developer Daniele Spinetti has released a new TForm class helper which loads and saves the state of the form in Delphi XE7 Firemonkey. One of the new features in XE7 on Android is a OnSaveState event which is occurs when a form is about to go to the background on mobile or be closed on the desktop. Daniele’s helper class adds a load and save serialization function for TForm and it’s child objects. Basically what this means is if the user entered a bunch of information on your form and then received a phone call on your mobile device it could save the state of all that entered information automatically. When the user comes back to the app it will load up that state information again and it will be like they never left. The information gets serialized and saved in JSON format. You can simply include the unit in your uses clause and then call the load and save functions. The helper class should work on Android, IOS, Windows, and OSX plus in Appmethod. Here is an example of the usage:

uses SaveStateHelper; procedure TForm1.FormCreate(Sender: TObject); begin LoadFormState; end; procedure TForm1.FormSaveState(Sender: TObject); begin SaveFormState; end; 1 2 3 4 5 6 7 8 9 10 11 uses SaveStateHelper ; procedure TForm1 . FormCreate ( Sender : TObject ) ; begin LoadFormState ; end ; procedure TForm1 . FormSaveState ( Sender : TObject ) ; begin SaveFormState ; end ;

And here is a sample from the source code of the different field types that it saves:

case FMXObj.Data.Kind of tkInteger: FMXJObj.AddPair(DATA_FIELD, TJSONNumber.Create(FMXObj.Data.AsInteger)); tkEnumeration: if FMXObj.Data.AsBoolean then FMXJObj.AddPair(DATA_FIELD, TJSONTrue.Create) else FMXJObj.AddPair(DATA_FIELD, TJSONFalse.Create); tkFloat: FMXJObj.AddPair(DATA_FIELD, TJSONNumber.Create(FMXObj.Data.AsExtended)); tkString, tkUString, tkLString, tkWString: FMXJObj.AddPair(DATA_FIELD, FMXObj.Data.AsString); tkInt64: FMXJObj.AddPair(DATA_FIELD, TJSONNumber.Create(FMXObj.Data.AsInt64)); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 case FMXObj . Data . Kind of tkInteger : FMXJObj . AddPair ( DATA_FIELD , TJSONNumber . Create ( FMXObj . Data . AsInteger ) ) ; tkEnumeration : if FMXObj . Data . AsBoolean then FMXJObj . AddPair ( DATA_FIELD , TJSONTrue . Create ) else FMXJObj . AddPair ( DATA_FIELD , TJSONFalse . Create ) ; tkFloat : FMXJObj . AddPair ( DATA_FIELD , TJSONNumber . Create ( FMXObj . Data . AsExtended ) ) ; tkString , tkUString , tkLString , tkWString : FMXJObj . AddPair ( DATA_FIELD , FMXObj . Data . AsString ) ; tkInt64 : FMXJObj . AddPair ( DATA_FIELD , TJSONNumber . Create ( FMXObj . Data . AsInt64 ) ) ;

Head over and download the free helper class for automatically saving form state in Delphi XE7 Firemonkey.

Mirror: You can also download the helper class archive here.