ApplicationWillDeactivateDelegate ApplicationHasReactivatedDelegate

Hi,

FCoreDelegates::ApplicationWillDeactivateDelegate
FCoreDelegates::ApplicationHasReactivatedDelegate

Is there a reason why these delegates are not called when the game window loses/gains focus on Android?

They only get called in pairs with
FCoreDelegates::ApplicationWillEnterBackgroundDelegate
FCoreDelegates::ApplicationHasEnteredForegroundDelegate

when the game activity gets stopped/started again.

See LaunchAndroid.cpp:
ActivateApp_EventThread()
SuspendApp_EventThread()

The Android platform does not provide a direct way to detect focus changes for an individual window. So it can’t call: FCoreDelegates::ApplicationWillDeactivateDelegate and FCoreDelegates::ApplicationHasReactivatedDelegate.

So you should use the onPause and onResume methods instead.

The FCoreDelegates::ApplicationWillEnterBackgroundDelegate and FCoreDelegates::ApplicationHasEnteredForegroundDelegate delegates are called by the engine in response to the onPause and onResume methods. These delegates are not directly related to the focus changes of the game window, but rather to changes in the overall state of the application.

If you need to detect focus changes for the game window on Android, you may need to use a different approach, such as listening for touch or key events.

After more investigation, I found a way to inform the game about focus changes.
In LaunchAndroid.cpp:

// callback that receives android_native_app_glue events
static void OnAppCommandCB(...)
{
    ...
    switch (cmd)
    {
    ...
    case APP_CMD_LOST_FOCUS:
        ...
        // broadcast the "focus lost" event
FAppEventManager::GetInstance()->EnqueueAppEvent(APP_EVENT_RUN_CALLBACK, 		FAppEventData([]()
        {
            FCoreDelegates::ApplicationWillDeactivateDelegate.Broadcast();
        }));
        break;
    case APP_CMD_GAINED_FOCUS:
        ...
        if (bHasWindow && bHasFocus && bIsResumed)
        {
            ...
            // broadcast the "focus gained" event
FAppEventManager::GetInstance()->EnqueueAppEvent(APP_EVENT_RUN_CALLBACK, FAppEventData([]()
            {
                FCoreDelegates::ApplicationHasReactivatedDelegate.Broadcast();
            }));
        }
        break;
    ...
    }
    ...
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.