Love, Death and the Lifecycle: Measuring How Much Time People Spend in Your Android App

Alexander Lebed11 Nov 2025

Problem

Analysts at the WB Partners app asked for four session events: process start, return from background, minimize and process end. Mapped one-to-one onto Android lifecycle callbacks, these events give wrong data. The system can kill the process without calling any callback, so "end" cannot be sent. The Application start callback also fires on background work, notifications and widget taps, which are not user sessions. Screen rotation recreates the Activity and re-fires the callbacks, and a crash skips them entirely.

Example from the article: a single-Activity app sends "show" in onStart and "hide" in onStop, and all callbacks fire again on every screen rotation although the user never left the app.

Solution in brief

First agree with analysts on what a session is: time from the app appearing on screen to its full hiding. That leaves two events, show and hide. Both are sent from one lifecycle observer that ignores configuration changes, sends hide from an uncaught-exception handler on crashes, and guards against duplicates and crashes that happen before the handler is installed.

Questions this case answers
  • How to log session-duration events on a client whose runtime gives no reliable callback when the app process is killed
  • How to define a user session from client-side show and hide events so that system-driven recreations are not counted as new sessions
  • How to send reliable session start and end events from an Android app for session-duration analytics?
  • How to avoid counting screen rotations and other configuration changes as new app sessions?

Tools and methods

  • Sessionization by client events — session = interval between show and hide events sent from the app.
  • Activity lifecycle observer — onStart/onStop events wrapped in a singleton class.
  • UncaughtExceptionHandler — sends hide when the app crashes, then calls the previous handler.
Continue reading — it's free

Google or a one-time email code. No password.