애플리케이션 이벤트핸들러를 등록하면 앱의 라이프 사이클 이벤트를 수신할 수 있습니다.
먼저 상단(interface) uses절에 FMX.Platform을 추가 후 이벤트 수신 용 이벤트를 아래와 같이 작성합니다.
function TForm1.AppEventHandler(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
begin
Result := False;
case AAppEvent of
TApplicationEvent.FinishedLaunching: Log.d('FinishedLaunching');
TApplicationEvent.BecameActive: Log.d('BecameActive');
TApplicationEvent.WillBecomeInactive: Log.d('WillBecomeInactive');
TApplicationEvent.EnteredBackground: Log.d('EnteredBackground');
TApplicationEvent.WillBecomeForeground: Log.d('WillBecomeForeground');
TApplicationEvent.WillTerminate: Log.d('WillTerminate');
TApplicationEvent.LowMemory: Log.d('LowMemory');
TApplicationEvent.TimeChange: Log.d('TimeChange');
TApplicationEvent.OpenURL: Log.d('OpenURL');
end;
end;메인 폼 생성 시 이벤트 핸들러를 등록(SetApplicationEventHandler)합니다.
procedure TForm1.FormCreate(Sender: TObject);
var
AEService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(AEService)) then
begin
AEService.SetApplicationEventHandler(AppEventHandler);
end;
end;아래 이벤트를 참고해 필요한 항목에 이벤트 발생 시 코드를 추가하세요.
애플리케이션 이벤트 플랫폼 별 지원 여부
이벤트 | 설명 | 안드로이드 지원 | iOS 지원 |
BecomeActive | 애플리케이션이 활성화 되었습니다. | 지원 | 지원 |
EnteredBackground | 애플리케이션이 백그라운드로 진입했습니다. | 지원 | 지원 |
FinishedLaunching | 애플리케이션이 시작되었습니다. | 지원 | 지원 |
LowMemory | 디바이스 메모리가 부족하다는 경고입니다. 내부 메모리 사용량을 줄이세요. | 지원 | 지원 |
OpenURL | url 요청을 받았습니다. | X | 지원 |
TimeChange | 시간에 큰 변화가 발생. 하루가 바뀐 경ㅇ나 서머타임이 시작 또는 끝난 경우 발생 | X | 지원 |
WillBecomeForeground | 백그라운드의 애플리케이션이 활성화 되었습니다. | 지원 | 지원 |
WillBecomeInactive | 다른 애플리케이션이 활성화 되었습니다. | 지원 | 지원 |
WillTerminate | 사용자가 애플리케이션을 종료합니다. | 지원 | 지원 |
안드로이드 라이프사이크
http://developer.android.com/reference/android/app/Activity.html
iOS 라이프사이크
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html