Post
by scottlynn » Mon 01 May 2017 14:23
No answer yet, and I was digging around looking for ways to handle the OnFormSaveState event, so here is what I am trying so far. It is an intermittent error message, so it will take a couple days of testing to determine if I have this fixed. I am trying to close the database connection in OnFormSaveState and then opening it again when my form is reactivated in Android, which requires adding an App event handler and registering it. If anyone else wants to try this, here is the code required to do it.
1) You need to add FMX.Platform to your uses clause.
2) You need something like the following in your OnCreate
var
aFMXApplicationEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent);
3) Declare the following as a procedure in the public declaration of your form:
function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
4) In OnFormSaveState close your database connection
5) Make sure your OpenDatabase routine is smart enough to check if it is already open or add a force open flag. Because it will be called once on program restore, but twice during a normal program startup.
6) Here is my HandleAppEvent. I have some extra comments that records the order of events I encountered while testing different restore scenarios. Enjoy. Note I am not doing anything on WillTerminate since I already handle those things in OnFormClose.
function TFormMain.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
case AAppEvent of
TApplicationEvent.aeBecameActive :
begin
// AddLogEvent('TAppEvent.aeBecameActive');
// Reconnect to database if not connected
DM.OpenDatabase(true);
Result := true;
end;
TApplicationEvent.aeWillTerminate :
begin
// AddLogEvent('TAppEvent.aeWillTerminate');
end;
// Have seen these
// TApplicationEvent.aeWillBecomeInactive : AddLogEvent('TAppEvent.aeWillBecomeActive');
// TApplicationEvent.aeEnteredBackground : AddLogEvent('TAppEvent.aeEnteredBackground');
// TApplicationEvent.aeWillBecomeForeground : AddLogEvent('TAppEvent.aeWillBecomeForeground');
// TApplicationEvent.aeFinishedLaunching : AddLogEvent('TAppEvent.aeFinishedLaunching');
// Haven't seen these yet
// TApplicationEvent.aeLowMemory : AddLogEvent('TAppEvent.aeLowMemory');
// TApplicationEvent.aeTimeChange : AddLogEvent('TAppEvent.aeTimeChange');
// TApplicationEvent.aeOpenURL : AddLogEvent('TAppEvent.aeOpenURL');
end;
// Startup sequence --> OnCreate, OnShow, OnActivate, aeFinishingLaunching, aeBecameActive
// Here is the order for a Home screen and then fast switching back to app
// aeEnteredBackground, OnFormSaveState, [fast switch to app], aeWillBecomeForeground, aeBecameActive
// Here is the order for a screen lock (dark) and then wakeup and unlock and app restore
// aeEnteredBackground, OnFormSaveState, [screen off then on], aeWillBecomeActive, aeWillBecomeForeground, aeBecameActive
end;