![fmxgrid | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/fmx_grid.jpg?resize=1140%2C628&ssl=1)
Author: Pawe Gowacki
The lifecycle of a mobile app is more complex and different from a desktop app. On a mobile device an application runs either in the foreground or in the background. When a phone calls arrive or when user opens another app, our app is going to background. If you are a programmer, you might be interested in responding to changes in the application state, for example saving current app state when your app goes into the background or refreshing the screen after moving into foreground.
Main two mobile operating systems, iOS and Android, fully support multitasking as their UNIX ancestor does. You would be surprised how many apps are constantly executing on your mobile device. On iOS just press the button below the screen twice, and a toolbar at the bottom of the screen will popup showing all executing apps. On Android devices you can also see all executing apps, but how you access the Task Manager may vary across devices. On my Nexus 7 tablet there is a special button next to “Home” button at the bottom of the screen to show the list of currently running apps.
The lifecycle of the application on Android is slightly different than on iOS. At the “developer” sections of web sites for both platforms you can find lifecycle charts. I have included them here for convenience.
Android app lifecycle is described at the http://developer.android.com/reference/android/app/Activity.html
iOS app lifecycle is described at the https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Delphi XE5 makes it possible use the same source code and just recompile your mobile app for Android or for iOS. There are differences in architecture of different services on different platforms. The FM Component Platform, also known as “FireMonkey”, contains “FMX.Platform” unit, which provides common interfaces to services on different platforms. It also contains an interface definition of a “IFMXApplicationEventService” that makes it possible register an event handler to receive app lifecycle events.
1 2 3 4 5 6 7 8 9 10 11 |
<strong>type</strong><br> TApplicationEvent = (aeFinishedLaunching, aeBecameActive, aeWillBecomeInactive,<br> aeEnteredBackground, aeWillBecomeForeground, aeWillTerminate, aeLowMemory,<br> aeTimeChange, aeOpenURL);<br> <br> TApplicationEventHandler = <strong>function</strong>(AAppEvent: TApplicationEvent; AContext: TObject): Boolean <strong>of</strong> <strong>object</strong>;<br> <br> IFMXApplicationEventService = <strong>interface</strong>(IInterface)<br> ['{F3AAF11A-1678-4CC6-A5BF-721A24A676FD}']<br> <strong> procedure</strong> SetApplicationEventHandler(AEventHandler: TApplicationEventHandler);<br> <strong> end</strong>; |
I just could not resist and decided to build a simple demo project to see what kinds of events my Delphi Mobile FireMonkey app would receive on different devices!
In your application code you need to define a function with the same signature as “TApplicationEventHandler”. Basically it can be any function that takes “TApplicationEvent” and “TObject” parameters and returns “boolean”.
Than you need to retrieve a reference to “IFMXApplicationEventService” from a global “TPlatformServices” class and register the app lifecycle event handler passing the reference to our event handler function as a parameter to “SetApplicationEventHandler” method of the service.
The “TPlatformServices” class contains “class” property called “Current” that returns the reference to “TPlatformServices” global, singleton implementation, so there is no need to instantiate it in our code.
Depending on the platform certain services may or may not be implemented, so you always need to check whether a given service is available. It is easy to use the following patterns to obtain references to different FireMonkey platform services. If the service is implemented in the “IFMXAService” interface, than we could use the following code snippet to access it:
1 2 3 4 5 6 7 8 9 |
<strong> var</strong> aFMXAService: IFMXAService;<br> <strong> begin</strong><br> <strong> if</strong> TPlatformServices.Current.SupportsPlatformService(IFMXAService, IInterface(aFMXAService)) <strong>then</strong><br> <strong> begin</strong><br> // call methods defined in the IFMXAService:<br> // aFMXAService.AMethod;<br> <strong> end</strong><br> <strong> else</strong><br> // FMXAService is not supported |
Below is full code for a simple demo form that just registers the app lifecycle event handler and the handler itself displays the name of the lifecycle event and the time it was received in a memo component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<strong>unit</strong> Unit11;<br> <br> <strong>interface</strong><br> <br> <strong>uses</strong><br> System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,<br> FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,<br> FMX.Memo, FMX.Platform, FMX.StdCtrls;<br> <br> <strong>type</strong><br> TForm11 = <strong>class</strong>(TForm)<br> Memo1: TMemo;<br> ToolBar1: TToolBar;<br> Label1: TLabel;<br> <strong> procedure</strong> FormCreate(Sender: TObject);<br> <strong> private</strong><br> <strong>procedure</strong> Log(s: <strong>string</strong>);<br> <strong>public</strong><br> <strong>function</strong> HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;<br> <strong>end</strong>;<br> <br> <strong>var</strong><br> Form11: TForm11;<br> <br> <strong>implementation</strong><br> <br> <em><em>{$R *.fmx}</em><br> <br> <em>{ TForm11 }</em></em><br> <br> <strong>procedure</strong> TForm11.FormCreate(Sender: TObject);<br> <strong>var</strong> aFMXApplicationEventService: IFMXApplicationEventService;<br> <strong>begin</strong><br> <strong>if</strong> TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) <strong>then</strong><br> aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)<br> <strong>else</strong><br> Log('Application Event Service is not supported.');<br> <strong>end</strong>;<br> <br> <strong>function</strong> TForm11.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;<br> <strong>begin</strong><br> <strong>case</strong> AAppEvent <strong>of</strong><br> aeFinishedLaunching: Log('Finished Launching');<br> aeBecameActive: Log('Became Active');<br> aeWillBecomeInactive: Log('Will Become Inactive');<br> aeEnteredBackground: Log('Entered Background');<br> aeWillBecomeForeground: Log('Will Become Foreground');<br> aeWillTerminate: Log('Will Terminate');<br> aeLowMemory: Log('Low Memory');<br> aeTimeChange: Log('Time Change');<br> aeOpenURL: Log('Open URL');<br> <strong>end</strong>;<br> Result := True;<br> <strong>end</strong>;<br> <br> <strong>procedure</strong> TForm11.Log(s: <strong>string</strong>);<br> <strong>begin</strong><br> Memo1.Lines.Add(TimeToStr(Now) + ': ' + s);<br> <strong>end</strong>;<br> <br> <strong>end</strong>. |
In the experiment I have compiled the application for “iOS Device” and “Android” targets. When the demo app started on the device, I have switched to another app and then back to the demo app.
In both cases the app has received “Became Active”, “Entered Background” and “Will Become Foreground” events. On Android the demo app additionally received “Finished Launching” event at the beginning of the execution “Will Become Inactive” just before receiving “Entered Background”.
Below there are screenshots from my iPhone 4S running iOS 6 and Nexus 2013 tablet running Android 4.3 running “LifecycleDemo” project.
The FM Component Framework (“FireMonkey”) provides common abstractions for different functionality supported on different operating systems. In this way we do not need to maintain different code bases for working with different APIs! This also means hiding the underlying complexity of individual API from a programmer, so the application code can be simpler and more easy to understand and maintain!
There are more cool services to explore in the “FMX.Framework” unit! Give it a try:-)
The source code for the “LifecycleDemo” can be downloaded from Code Central.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition