How can I keep my application in the foreground?

  

Question: How do I keep my application in the foreground and/or prevent it from going to the background ever?

I have created an application and managed to get it to automatically start when the device has booted. The purpose of the app is to show some data and never ever go to the menu or terminate unless I want it too.

Every now and then the ActivityManager decides that my app is not important enough and wants to send it to the background or even worse, kill it entirely. Sometimes it happens after 1, 4 or 16 hours.

So far I have found out how to check if the app receives various ApplicationEvents with:

function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
case AAppEvent of
TApplicationEvent.FinishedLaunching: MemoPrint(‘Finished Launching’);
TApplicationEvent.BecameActive: MemoPrint(‘Became Active’);
TApplicationEvent.WillBecomeInactive: MemoPrint(‘Will Become Inactive’);
TApplicationEvent.EnteredBackground: MemoPrint(‘Entered Background’);
TApplicationEvent.WillBecomeForeground: MemoPrint(‘Will Become Foreground’);
TApplicationEvent.WillTerminate: MemoPrint(‘Will Terminate’);
TApplicationEvent.LowMemory: MemoPrint(‘Low Memory’);
end;
Result := True;
end;

procedure TForm1.LinkAppEvent;
var
aFMXApplicationEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService,
IInterface(aFMXApplicationEventService)) then
aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
else
MemoPrint(‘Application Event Service is not supported.’);
end;

So I would like to know how to proceed from here. Hopefully I was on the right track so far. 🙂

Comments are closed.