App Lifecycle

The application lifecycle has four states:

  • Foreground: when the user interface is displayed
  • Background: when the remote player is playing full screen video
  • Transition to Foreground: in process of switching to the user interface
  • Transition to Background: in process of switching to the remote player

State Management

In background mode, after a period of time the application is unloaded to save resources. It will be reloaded on the next key press, end-of-stream event, error event or request of a DRM license (either content or renewal).

In order for the user to resume where they left off, your app should preserve its state as follows:

  1. Before switching to the background, save the current state to local storage or session storage.
  2. After switching to the foreground, restore the state from local or session storage if the app has been restarted.

πŸ“˜

State Storage

See the guide on Preserving State to learn more about persistency of storage across UI release and reboots.

The sections below explain the Client Library APIs for App Lifecycle management.

Modules

lifecycle β‡’ Lifecycle

Classes

Lifecycle

Lifecycle is a singleton class that manages the application lifecycle states:

Typedefs

State : string
  1. 'foreground' - ui is displayed
  2. 'inTransitionToForeground' - ui is about to be displayed
  3. 'background' - remote player is playing (full screen playback is displayed)
  4. 'inTransitionToBackground' - remote player is about to be playing

lifecycle β‡’ Lifecycle

Returns: Lifecycle - pointer to the Lifecycle singleton
Example

import { lifecycle } from "senza-sdk";

Lifecycle

Lifecycle is a singleton class that manages the application lifecycle states:

Kind: global class
Emits: event:onstatechange

lifecycle.getState() β‡’ State

Async function that returns the ui lifecycle state

Kind: instance method of Lifecycle
Returns: State - the current application lifecycle state
Example

try {
    const state = await lifecycle.getState();
    console.log("current state is", state);
} catch (e) {
    console.error("getState failed", e);
}

lifecycle.moveToForeground()

Once playback starts on the remote player, the application is moved from foreground to inTransitionToBackground and eventually to background. The application will need to call moveToForeground when it receives an event that needs the UI to be displayed again, for example a key press, a playback end-of-file or a playback error.

Kind: instance method of Lifecycle

lifecycle.switchTenant(tenantId) β‡’ Promise

Use this api to switch to another tenant (other than the home tenant) which will launch the application associated with the tenantId. The tenantId must be configured in the hyperscale platform. Switching to the home tenant should use the exitApplication().

Kind: instance method of Lifecycle
Returns: Promise - Promise which is resolved when the switchTenant command has been successfully processed. Failure to process the switchTenant command will result in the promise being rejected.

ParamTypeDescription
tenantIdstringThe tenantId to switch to

lifecycle.exitApplication() β‡’ Promise

Use this api to exit the application which will redirect the browser to the home tenant application. In the case of a Content Provider application part of the Content Hub, this means returning to the Content Hub tenant.

Kind: instance method of Lifecycle
Returns: Promise - Promise which is resolved when the exitApplication command has been successfully processed. Failure to process the exitApplication command will result in the promise being rejected.

"onstatechange"

Fired after transition from one state to another.

The flow is: foreground --> inTransitionToBackground --> background --> inTransitionToForeground --> foreground

Kind: event emitted by Lifecycle
Properties

NameTypeDescription
stateStateIndicates the new state.

Example

lifecycle.addEventListener("onstatechange", (e) => {
    console.log("new state is", e.state);
});

State : string

  1. 'foreground' - ui is displayed
  2. 'inTransitionToForeground' - ui is about to be displayed
  3. 'background' - remote player is playing (full screen playback is displayed)
  4. 'inTransitionToBackground' - remote player is about to be playing

Kind: global typedef


What’s Next