How can I get iOS events such as applicationDidBecomeActive?

Hello,
How can I get iOS events such as applicationDidBecomeActive, applicationWillResignActive, applicationDidEnterBackground and applicationWillEnterForeground ?

After looking around the engine source, I found UApplicationLifecycleComponent and FCoreDelegates::ApplicationWillDeactivateDelegate (and others.)
But I don’t know how to use them. If anyone can give me some examples, I would be really grateful.

I also wonder UApplicationLifecycleComponent or FCoreDelegates works for Android.
Thanks.

It’s seems it a actor component which so you can attach to any actor, in that components there a delegates, which actually are usable in blueprints it seems. To attach component place in

.h

UPROPERTY()
TSubobjectPtr<UApplicationLifecycleComponent> Lifecycle;

In constructor of class:

Lifecycle = PCIP.CreateDefaultSubobject<UApplicationLifecycleComponent>(this, TEXT("Lifecycle"));
Lifecycle->bIsActive = true;
AddComponent(FName("Lifecycle"), false, FTransform(), NULL);

and then in overrided PostInitializeComponents() or BeginPlay() you bind delegates to a function you which to be triggered in event, for example like this

Lifecycle->ApplicationWillEnterBackgroundDelegate.AddDynamic(this,&ASomeClass::SomeFunction);

If that does not owrk try BindDynamic, First argument is instance of class with function you want to call 2nd is pointer to function you want event to call. Keep in mind that to make your target function to be marked with UFUNCTION() or else it won’t work and game will start to behave strange.

I think best place to attach this component will be AGameMode which controls your game

I know that in Android, Unreal still uses CPU on sleep, so im not 100% sure if those events works on Android :stuck_out_tongue:

Thanks a lot! I’ve successfully integrated your codes to mine.

By the way, it looks like applicationDidEnterBackground and applicationWillEnterForeground are not called like common iOS app. They are called at odd timing.

Fortunately applicationDidBecomeActive and applicationWillResignActive are called right timing, so I’ll use them.

FCoreDelegates are not yet implemented on Android.

All of the applicationXXXX delegates are called directly from the corresponding event from the app delegate on iOS. However, because it is calling our internal delegates, the even may be getting pushed to our main thread which is a different thread than the delegate received the event on, so that may account for the odd timing.

-Pete

I love you!
It’s works on Android . Thank you .!!!