Firemonkey: How to detect when the OS (Android/iOS) is going to shutdown

551 Views Asked by At

I am dealing with the following issue with Firemonkey (Delphi 10.4): When the Android OS will shut down and my app is still running, it does not trigger the OnCloseQuery, OnClose, and nor the OnDestroy events. Is there a way to detect or intercept the OS shutdown event? The same issue is presented when I kill the app with the square button (that is when I show the recently opened apps with the square button and I close the app that way).

Thank you in advance.

1

There are 1 best solutions below

1
LUIS A. GAMA On

I finally found a solution from a TMS customer (Ken Randall "Randall_Ken" Active Customer.)

uses FMX.Platform;

procedure TMyForm.FormCreate(Sender: TObject);
var
  AppEventSvc: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService
    (IFMXApplicationEventService, IInterface(AppEventSvc)) then
  begin
    AppEventSvc.SetApplicationEventHandler(AppEvent);
  end;
end;

function TMyForm.AppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
begin
  if AAppEvent = TApplicationEvent.WillTerminate then
  begin
    // Do soomething
  end;
  Result := true;
end;