App Lifecycle

As the other popular apps in various platforms, webOS TV app also has its own lifecycle. After an app is launched by user, the status of app is changed continuously by specific conditions until terminated. That is why you need to consider how to handle the lifecycle in case of status changed.

This sample app just shows the result of each event and the case of handlesRelaunch option to set the value true is not considered.

If you want to know more details about app lifecycle, see App Lifecycle.

Adding Event Listener for webOSLaunch Event

To handle webOSLaunch event, you have to add event listener to document object. This sample app shows a message when the app is launched.

document.addEventListener('webOSLaunch', function(inData) {
	var launchElement = document.getElementById("launch");
	launchElement.innerHTML = "App Launched";
}, true);

Adding Event Listener for webOSRelaunch Event

To handle webOSRelaunch event, you also have to add event listener to document object. This sample app shows a message when the app is launched.

document.addEventListener('webOSRelaunch', function(inData) {
	var launchElement = document.getElementById("reLaunch");
	launchElement.innerHTML = "App Relaunched";
}, true);

Adding Event Listener for visibilityChange Event

The visibilityChange event is fired when a web app changes from visible to hidden or vice versa. The webOS TV sends an onwebkitvisibilitychange event to the app and the app can have an event handler to manage the actions for its visible or hidden state.

 

You can add an event handler for each standard web engine and webkit. For the backward compatibility, adding an event handler for the standard web engine is recommended.

var hidden, visibilityChange; 

if (typeof document.hidden !== "undefined") {
	hidden = "hidden";
	visibilityChange = "visibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
	hidden = "webkitHidden";
	visibilityChange = "webkitvisibilitychange";
}

 

To handle visibilityChange event, you have to add event listener to document object. This sample app shows a message when the visibility of app is changed.

document.addEventListener(visibilityChange, function() { 
	if (document[hidden]) {
		var visibilityElement = document.getElementById("visibility");
		visibilityElement.innerHTML = "App Hidden";
	}
	else {
		var visibilityElement = document.getElementById("visibility");
		visibilityElement.innerHTML = "App Shown";
	}
}, true);
You can’t see the result of visibility event in the webOS TV emulator or webOS TV. If you want to check the result, we recommend you to use the chrome inspector.

Download

For your understanding on the lifecyle of webOS TV app, you can download the sample code and its IPK file at the below link for free.

Do's and Don'ts

  • Do set "handleRelaunch" option in appinfo.json file to true, then test it again.
  • Do check result of visibilityChange event using the chrome inspector.
  • Don't test this sample app on the web browser.
Navigation