How to get app resume state on iOS and Android?

折月煮酒 提交于 2019-12-04 11:11:11

You need to use IFMXApplicationEventService to register a callback where the application will be notified:

uses FMX.Types, FMX.Platform;

function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
  case AAppEvent of
    TApplicationEvent.FinishedLaunching:    Log.d('Launched.');
    TApplicationEvent.BecameActive:         Log.d('Gained focus.');
    TApplicationEvent.EnteredBackground:    Log.d('Now running in background.');
    TApplicationEvent.WillBecomeForeground: Log.d('Restoring from background.');
    TApplicationEvent.WillBecomeInactive:   Log.d('Going to lose focus.');
    TApplicationEvent.WillTerminate:        Log.d('Quitting the application.');
    TApplicationEvent.LowMemory:            Log.d('Device running out of memory.');

    // iOS only
    TApplicationEvent.TimeChange:           Log.d('Significant change in time.');
    TApplicationEvent.OpenURL:              Log.d('Request to open an URL.');
  end;

  Result := True;
end;

procedure TForm11.FormCreate(Sender: TObject);
var
  aFMXApplicationEventService: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService,
    IInterface(aFMXApplicationEventService))
  then
    aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
  else
    Log.d('Application Event Service not supported.');
end;

More info about the event types here.

A good article on the subject by Paweł Głowacki (for Delphi XE5, but still useful).

In iOS You can add flag in

applicationDidEnterBackground

in appDelegate to know if the user enters the background and,

applicationDidBecomeActive

to know that the user returns to the app from background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!