Lifecycle
The application lifecycle has four states:
![](https://files.readme.io/bea7ea8-App_lifecycle_1.png)
- 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:
- Before switching to the background, save the current state to local storage or session storage.
- 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
Classes
- Lifecycle
Lifecycle is a singleton class that manages the application lifecycle states.
Typedefs
- UiState :
Object
The ui lifecycle state
lifecycle β 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
, event:userinactivity
- Lifecycle
- .state β
UiState
.getState() βUiState
- .moveToForeground() β
Promise
- .moveToBackground() β
Promise
- .moveToUiStandby()
- .switchTenant(tenantId) β
Promise
- .exitApplication() β
Promise
- "onstatechange"
- "userinactivity"
- .state β
lifecycle.state β UiState
UiState
Getter for returning the ui lifecycle state
Kind: instance property of Lifecycle
Returns: UiState
- the current application lifecycle state
lifecycle.getState() β UiState
UiState
Deprecated
Kind: instance method of Lifecycle
Returns: UiState
- 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() β Promise
Promise
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
Returns: Promise
- Promise which is resolved when the moveToForeground command has been successfully processed.
Failure to process the moveToForeground command will result in the promise being rejected.
lifecycle.moveToBackground() β Promise
Promise
This method moves the application to the background.
It should be called after remotePlayer.play().
As a consequence, remote player playback will be displayed in full screen.
Kind: instance method of Lifecycle
Returns: Promise
- Promise which is resolved when the moveToBackground command has been successfully processed.
Failure to process the moveToBackground command will result in the promise being rejected.
Example
remotePlayer.load("https://example.com/video.mp4", 0);
remotePlayer.play();
lifecycle.moveToBackground();
lifecycle.moveToUiStandby()
This method moves the application into standby mode, i.e. last ui frame is displayed and ui resources are released.
It should be called after "userinactivity" event is triggered on the lifecycle module.
Kind: instance method of Lifecycle
Alpha: API has not yet been released
Example
lifecycle.addEventListener("userinactivity", () => {
lifecycle.moveToUiStandby();
}
lifecycle.switchTenant(tenantId) β Promise
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
Senza 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.
Param | Type | Description |
---|---|---|
tenantId | string | The tenantId to switch |
lifecycle.exitApplication() β Promise
Promise
Use this api to exit the application which will redirect the browser to the home tenant application.
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
Name | Type | Description |
---|---|---|
state | UiState | Indicates the new state. |
Example
lifecycle.addEventListener("onstatechange", (e) => {
console.log("new state is", e.state);
});
"userinactivity"
Fired after the ui has been inactive (i.e. no key presses) for a configurable number of seconds.
Kind: event emitted by Lifecycle
Alpha: API has not yet been released
Properties
Name | Type | Description |
---|---|---|
timeout | number | the number of seconds after which the application will be unloaded. |
Example
lifecycle.addEventListener("userinactivity", (e) => {
console.log(`Application will be unloaded in ${e.timeout} seconds`);
});
UiState : Object
Object
The ui lifecycle state
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
UNKNOWN | string | state is unknown at this time |
FOREGROUND | string | ui is displayed |
IN_TRANSITION_TO_FOREGROUND | string | ui is about to be displayed |
BACKGROUND | string | remote player is playing (full screen playback is displayed) |
IN_TRANSITION_TO_BACKGROUND | string | remote player is about to be playing |
Updated 22 days ago