For AI agents: a Markdown version of this page is at https://docs.snowplow.io/docs/sources/mobile-trackers/tracking-events/lifecycle-tracking.md. The index of all documentation pages is at https://docs.snowplow.io/llms.txt, and the index for this section is at https://docs.snowplow.io/docs/cdi/llms.txt.
Track application lifecycle changes with the native mobile trackers
The tracker can capture application lifecycle state changes. In particular, when the app changes state from foreground to background and vice versa.
The lifecycle tracking is enabled by default (since v6.0.0). It can be configured in TrackerConfiguration like in the example below:
- iOS
- Android (Kotlin)
- Android (Java)
let trackerConfig = TrackerConfiguration()
.lifecycleAutotracking(true)
val trackerConfig = TrackerConfiguration("appId")
.lifecycleAutotracking(true)
TrackerConfiguration trackerConfig = new TrackerConfiguration("appId")
.lifecycleAutotracking(true);
Once enabled, the tracker will automatically track a Background event when the app is moved to background and a Foreground event when the app moves back to foreground (becomes visible in the screen).
The tracker doesn't track a Foreground event when the user launches the app, only when the app returns from the background.
The system can also launch an app in the background, for example for a silent push notification. Events tracked during a background launch report the app as not visible. When the user then opens the app, the trackers behave differently:
- iOS: the tracker doesn't track a
Foregroundevent, because it only tracks one after aBackgroundevent - Android: the tracker tracks a
Foregroundevent
Lifecycle state on background launches was fixed in version 6.2.6 of the iOS tracker and version 6.4.1 of the Android tracker. iOS tracker version 6.2.6 to 6.3.0 also tracked a Foreground event on every launch of a scene-based app, which version 6.3.1 fixed.
The tracker attaches a LifecycleEntity to all the events tracked by the tracker reporting if the app was visible (foreground state) when the event was tracked.
The LifecycleEntity value is conditioned by the internal state of the tracker only. To make an example, if the app is in foreground state but the developer tracks a Background event intentionally, it would force the generation of a LifecycleEntity that mark the app as non visible, even if it's actually visible in the device.