Lifecycle Events

The Lifecycle object emits a number of events that your app can respond to. See the Lifecycle class documentation for complete details.

State change

The Lifecycle class has four states:

  • Foreground
  • In Transition to Background
  • Background
  • In Transition to Foreground

Your app can listen for an onstatechange event like this:

senza.lifecycle.addEventListener("onstatechange", (event) => {
    console.log("Lifecycle state:", event.state);
});

See the Stopwatch tutorial for an example that shows how to listen for lifecycle state changes.

User inactivity

To conserve server resources, Senza includes an automatic sleep feature that activates when the device hasn’t been used for an extended period. They may have stopped watching, but they may also just be watching a long movie.

Senza tracks user activity and, by default, will attempt to disconnect the session after three hours of inactivity. Before doing so, it sends a message to your app that asks, "Are you still there?" If the user responds by pressing any button, the inactivity timer resets. If they don’t, the session will disconnect after the specified countdown.

senza.lifecycle.addEventListener("userinactivity", (e) => {
    showMessage(`Are you still there? Press any button in the next ${e.timeout} seconds to keep watching.`);
});

To test this feature in your app without waiting the full three hours, you can manually simulate an inactivity warning by dispatching a fake event.

senza.lifecycle.dispatchEvent(new CustomEvent("userinactivity", {detail: {timeout: 30}}));

User disconnected

When a user session ends—either due to prolonged inactivity, switching away from the app, or explicitly exiting—the Senza platform triggers a userdisconnected event. This event signals that the current session is no longer active and that your app should begin cleaning up any associated resources.

Handling this event ensures that your app doesn't continue to run background processes or maintain stale state once the user is gone. It’s also a good point to reset UI elements, release memory, or disconnect from any services that are no longer needed.

senza.lifecycle.addEventListener("userdisconnected", () => {
    console.log("User session ended, cleaning up application state");
    // Perform any necessary cleanup here
});