Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiDemoFiremonkeyIOS

Handle Android And IOS Lifecycle Events In Delphi XE5 Firemonkey

Delphi Firemonkey Android IOS Application LifecycleUnlike standard Delphi desktop applications, the lifecycle of a mobile app is different and more complex. Also, the lifecycle of an Android application is slightly different than that of an iOS application. On both Android and iOS mobile devices, an application runs either in the foreground or in the background. For example, when a phone call arrives or when the user opens another application on the device, your application will go to the background. As a programmer, you may need to respond to these life cycle events. For example, you may need to save your current application state when your application is sent to the background or repaint the screen when moving back to the foreground. Using Delphi XE5, it is possible to use the same source code and compile your mobile application for Android and iOS. Although there are differences in their architectures, FireMonkey contains the “FMX.Platform” unit, which contains an interface definition of a “IFMXApplicationEventService” that makes it possible register an event handler to receive application lifecycle events for both platforms. Delphi XE5 version:

function TForm11.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
  case AAppEvent of
    aeFinishedLaunching: Log('Finished Launching');
    aeBecameActive: Log('Became Active');
    aeWillBecomeInactive: Log('Will Become Inactive');
    aeEnteredBackground: Log('Entered Background');
    aeWillBecomeForeground: Log('Will Become Foreground');
    aeWillTerminate: Log('Will Terminate');
    aeLowMemory: Log('Low Memory');
    aeTimeChange: Log('Time Change');
    aeOpenURL: Log('Open URL');
  Result := True;

Update: Delphi XE6 and Delphi XE7 version:

function TForm11.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
  case AAppEvent of
    FinishedLaunching: Log('Finished Launching');
    BecameActive: Log('Became Active');
    WillBecomeInactive: Log('Will Become Inactive');
    EnteredBackground: Log('Entered Background');
    WillBecomeForeground: Log('Will Become Foreground');
    WillTerminate: Log('Will Terminate');
    LowMemory: Log('Low Memory');
    TimeChange: Log('Time Change');
    OpenURL: Log('Open URL');
  Result := True;

Stop by Embarcadero’s Delphi Programming blog and review the full code and download the simple demo


Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes


Ryzen 9 5950x: One Billion Lines Of Delphi Code Compiled In ~5 Minutes On 16 Cores


DelphiCon 2020 FireMonkey Sessions Available Starting November 19


Powerful Commercial Gorilla3D Framework Upgraded For Delphi FireMonkey On Windows And Android

Sign up for our Newsletter and
stay informed

Leave a Reply