September 15 — Topics: Developing with the webOS SDK — enda

A neat little feature in webOS is the ability to push a scene from another application on top of your applications stack. Some of the core apps that have this feature are the People Picker (which is a cross-launch of the contacts app), the camera capture, and the video player.

The major advantages of doing this is are:

Not having the overhead of launching another app using the application manager (waiting for start-up, etc.) and
Not losing navigation context from your app, i.e., the user doesn’t have to minimize or close the launched app and then re-select your app. A “back” swipe to return to the previous scene will do the trick.
In this post I’m going to show how to add this feature to your app. I’ll start by creating two apps. The first one, App A, will be the “launcher” app. This app, using the cross-app API, will push a scene from App B onto its stack, displaying information passed from App A. If App B is launched from the launcher the same scene will display information about App B.


App B – Normal launch App A App B pushed from App A
Both apps are straightforward: I used the helloworld App template to create the apps, and made two changes. In the appinfo.json, the only change from a standard app is that App B has "noWindow": true. This stops App B from being launched on a new stage when pushed from another app. The other obvious difference is that App B can be launched with parameters, where App A can’t, so to handle this I replaced the stage-assistant with an app-assistant.

If both apps are launched normally from the launcher they both act the same—the first scene is pushed and information about the app is displayed (see images on the left and center above). In this case it’s the App’s title, ID, and version. However, if the “Cross Launch” button in App A is tapped, it will push the scene from App B onto its stack (image on the right above).

FirstAssistant.prototype.handleButtonPressed = function(event) {
var args = {
appId: "com.palmdts.appb",
name: "first"
};
var params = {
title : Mojo.appInfo.title,
id : Mojo.appInfo.id,
version : Mojo.appInfo.version
};
this.controller.stageController.pushScene(args, params);
}
The magic happens in the call to pushScene; instead of just specifying the usual scene name to push, it’s passed two objects. The first parameter contains the app ID and scene name of the app we want to cross-launch. In this example, we’re launching App B’s first scene, and the second parameter contains the information we want to pass.

When App B is crossed-launched, the parameters are passed to the scene just like they would be passed by a scene in its own app—in the constructor. Taking a look at the code, you can see that what is displayed in the scene depends on whether anything was passed to it or not.

function FirstAssistant(args) {
this.launchParams = {};
if (args) {
this.launchParams = args;
this.str1 = 'This app was launched by another app!';
this.str2 = 'Info passed from launcher app';
}
else {
this.launchParams.title = Mojo.appInfo.title;
this.launchParams.id = Mojo.appInfo.id;
this.launchParams.version = Mojo.appInfo.version;
this.str1 = 'This app was launched from the launcher';
this.str2 = 'Mojo info for app B';
}
}
That’s all there is to it. I have posted the code here on github so you can try it yourself. Have fun!



More information and screen shots on the link:


Cross-app API implementation | Palm Developer Center Blog