Skip to content

Extend session tracking to startup #2054

@bitsandfoxes

Description

@bitsandfoxes

Problem

Currently, when users put the app in the background during startup, the SDK has no way to compensate for the inactivity, still finishing the transaction based on timestamps. Which can result in reporting unrealistically long startups (i.e. 4hrs).

Context

The SDK relies on MonoBehaviour's built-in functionality to get notified about the app's state OnApplicationPause and OnApplicationFocus here:

/// <summary>
/// To receive Pause events.
/// </summary>
internal void OnApplicationPause(bool pauseStatus) => UpdatePauseStatus(pauseStatus);
/// <summary>
/// To receive Focus events.
/// </summary>
/// <param name="hasFocus"></param>
internal void OnApplicationFocus(bool hasFocus) => UpdatePauseStatus(!hasFocus);

This creates a gap in the SDK's coverage during startup since the MonoBehaviour has not loaded yet.

Proposal

Instead, the SDK should move that functionality outside of the MonoBehaviour and make use of Application.focusChanged. Since this is supported only on Unity 2021 and newer we'll have to make this conditional. This does not work. The Application.focusChanged is only ever invoked after Awake and before Start.

It looks like we'd need to build our own integration for this.

Android

We can implement our own hook into ActivityLifeCycleCallbacks.

using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (var application = activity.Call<AndroidJavaObject>("getApplication"))
{
    AndroidJavaProxy lifecycleCallbacks = new AndroidLifecycleCallbacks();
    application.Call("registerActivityLifecycleCallbacks", lifecycleCallbacks);
}

iOS

TBD

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions