![]() |
AW: versehentliches Beenden einer Android App
Du musst die Livecycle Events deiner App berücksichtigen!
Delphi-Quellcode:
Und
procedure TAppForm.FormCreate(Sender: TObject);
Begin //Intercept LifeCycleEvents var aFMXApplicationEventService:IFMXApplicationEventService if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService) ) then aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent) else Tlog.d('Application Event Service is not supported.'); end;
Delphi-Quellcode:
Dadurch kannst du alle Eingaben sichern befor die App beendet wird und die Eingaben wieder laden und anzeigen wenn die App fortgesetzt wird und nichts geht verloren
function TAppForm.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin case AAppEvent of TApplicationEvent.FinishedLaunching: TLog.d('FinishedLaunching'); // Only Android, Probably IOS too (App has been started by the user) TApplicationEvent.BecameActive: TLog.d('BecameActive'); // Both (App is in USE) TApplicationEvent.WillBecomeInactive: TLog.d('WillBecomeInactive'); // Only Android, Probably IOS too (Focus is about to switch to other app) TApplicationEvent.EnteredBackground: // Both (App is beeing executed while in the Background) Begin TLog.d('EnteredBackground'); TAppViewModel.getinstance.EnterBackground; End; TApplicationEvent.WillBecomeForeground: // Both (App is focused) Begin TLog.d('WillBecomeForeground') ; TAppViewModel.getinstance.EnterForeground; End; TApplicationEvent.WillTerminate:TLog.d('WillTerminate') ; // Both (User terminted the APP) TApplicationEvent.LowMemory: TLog.d('LowMemory'); // Both (Please use less memory, please! But how?) TApplicationEvent.TimeChange:TLog.d('TimeChange') ; // IOS TApplicationEvent.OpenURL:TLog.d('OpenURL') ; // IOS end; Result := True; end; |
AW: versehentliches Beenden einer Android App
Hi,
wo kommt denn das TAppViewModel her? |
AW: versehentliches Beenden einer Android App
Zitat:
TAppForm ist das Main-Formular der APP
Delphi-Quellcode:
ist der prozedur kopf eines OnFormCreate Ereignisses auf dem MainFormular.
procedure TAppForm.FormCreate(Sender: TObject);
Delphi-Quellcode:
Dieser Code hinterlegt eine Methode HandleAppEvent als Ereignis Handler für LifeCycleEvents einer Android App.
//Intercept LifeCycleEvents
var aFMXApplicationEventService:IFMXApplicationEventService if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService) ) then aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent) else Tlog.d('Application Event Service is not supported.'); Immer wenn ein solches Event auf tritt wird die Methode HandleAppEvent von Android benachrichtigt , damit du darauf reagieren kannst.
Delphi-Quellcode:
Das ist der Methoden kopf der Methode die wir als EventHandler hinterlegt haben.
function TAppForm.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
Delphi-Quellcode:
Das ist ein Beispiel code dessen was man an Events im Eventhandler abfangen kann. Ich logge die meisten Events nur und wenn die Anwendung in den Hintergrund gerät schalte ich NFC und GPS ab und wenn sie in den Vordergrund kommt schalte ich es wieder ein. Ich schone damit den Akku. Für dich ist das vielleicht gar nicht wichtig .
case AAppEvent of
TApplicationEvent.FinishedLaunching: TLog.d('FinishedLaunching'); // Only Android, Probably IOS too (App has been started by the user) TApplicationEvent.BecameActive: TLog.d('BecameActive'); // Both (App is in USE) TApplicationEvent.WillBecomeInactive: TLog.d('WillBecomeInactive'); // Only Android, Probably IOS too (Focus is about to switch to other app) TApplicationEvent.EnteredBackground: // Both (App is beeing executed while in the Background) Begin TLog.d('EnteredBackground'); TAppViewModel.getinstance.EnterBackground; End; TApplicationEvent.WillBecomeForeground: // Both (App is focused) Begin TLog.d('WillBecomeForeground') ; TAppViewModel.getinstance.EnterForeground; End; TApplicationEvent.WillTerminate:TLog.d('WillTerminate') ; // Both (User terminted the APP) TApplicationEvent.LowMemory: TLog.d('LowMemory'); // Both (Please use less memory, please! But how?) TApplicationEvent.TimeChange:TLog.d('TimeChange') ; // IOS TApplicationEvent.OpenURL:TLog.d('OpenURL') ; // IOS end; Result := True;
Delphi-Quellcode:
Das Event aber vielleicht schon. Wenn dieses Event kommt kannst du noch eingreifen befor deine App stirbt.
TApplicationEvent.WillTerminate:TLog.d('WillTerminate') ; // Both (User terminted the APP)
Wenn du alle livecycle Events und das onException event deiner App loggs ist das vielleicht auch hilfreich. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:02 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz