From 86beebb1763dfaec6670c09352436bab3a7922b6 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Wed, 14 Jan 2015 10:19:26 +0200 Subject: [PATCH] Added application-management help file. --- application-management.md | 110 ++++++++++++++++++++++++++++++++++++++ application.md | 38 ------------- navigation.md | 3 +- 3 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 application-management.md delete mode 100644 application.md diff --git a/application-management.md b/application-management.md new file mode 100644 index 000000000..aef58f605 --- /dev/null +++ b/application-management.md @@ -0,0 +1,110 @@ +--- +nav-title: "Application Management" +title: "Application Management" +description: "Application Management" +position: 2 +--- + +# Persist and Restore Application Settings +To persist settings that the user has defined you have to use the local-settings module. The local-settings module is a static singleton hash table that stores key-value pairs for the application. The getter methods have two parameters -- a key and an optional default value to return if the specified key does not exist. The setter methods also have two parameters -- key and value. Here is an example of using the local settings-module and all of its available methods: +``` JavaScript +var localSettings = require("local-settings"); +// Event handler for Page "loaded" event attached in main-page.xml +function pageLoaded(args) { + localSettings.setString("Name", "John Doe"); + console.log(localSettings.getString("Name")); // Prints "John Doe" + localSettings.setBoolean("Married", false); + console.log(localSettings.getBoolean("Married")); // Prints false + localSettings.setNumber("Age", 42); + console.log(localSettings.getNumber("Age")); // Prints 42 + console.log(localSettings.hasKey("Name")); // Prints true + localSettings.remove("Name"); // Removes the Name entry. + console.log(localSettings.hasKey("Name")); // Prints false +} +exports.pageLoaded = pageLoaded; +``` +``` TypeScript +import observable = require("data/observable"); +import localSettings = require("local-settings"); +// Event handler for Page "loaded" event attached in main-page.xml +export function pageLoaded(args: observable.EventData) { + localSettings.setString("Name", "John Doe"); + console.log(localSettings.getString("Name"));// Prints "John Doe" + localSettings.setBoolean("Married", false); + console.log(localSettings.getBoolean("Married"));// Prints false + localSettings.setNumber("Age", 42); + console.log(localSettings.getNumber("Age"));// Prints 42 + console.log(localSettings.hasKey("Name"));// Prints true + localSettings.remove("Name");// Removes the Name entry. + console.log(localSettings.hasKey("Name"));// Prints false +} +``` +# Using Application Callbacks +Each NativeScript application has several important lifecycle events. You can use those events to perform all kinds of needed maintanance and housekeeping: ++ onLaunch(context) - method called when application launch. ++ onSuspend() - method called when the application is suspended. ++ onResume() - method called when the application is resumed after it has been suspended. ++ onExit() - method called when the application is about to exit. ++ onLowMemory() - method called when there is low memory on the target device. ++ onLowMemory(error) - method called when there is an uncaught application error. +Here is a code example that demonstrates how too use those callback functions: +``` JavaScript +var application = require("application"); +application.mainModule = "app/template-settings/main-page"; +application.onLaunch = function (context) { + // For Android applications the context is an android.content.Intent. + // For iOS applications the context is undefined, i.e. there is no available context. + if (application.android) { + console.log("Launched Android application with intent: " + context); + } + else if (application.ios) { + console.log("Launched iOS application"); + } +}; +application.onSuspend = function () { + console.log("Application suspended"); +}; +application.onResume = function () { + console.log("Application resumed"); +}; +application.onExit = function () { + console.log("Application exit"); +}; +application.onLowMemory = function () { + console.log("Application low memory"); +}; +application.onUncaughtError = function (error) { + console.log("Application error: " + error.name + "; " + error.message + "; " + error.nativeError); +}; +application.start(); +``` +``` TypeScript +import application = require("application"); +application.mainModule = "app/main-page"; +application.onLaunch = function (context: any) { + // For Android applications the context is an android.content.Intent. + // For iOS applications the context is undefined, i.e. there is no available context. + if (application.android) { + console.log("Launched Android application with intent: " + context); + } + else if (application.ios) { + console.log("Launched iOS application"); + } +} +application.onSuspend = function () { + console.log("Application suspended"); +} +application.onResume = function () { + console.log("Application resumed"); +} +application.onExit = function () { + console.log("Application exit"); +} +application.onLowMemory = function () { + console.log("Application low memory"); +} +application.onUncaughtError = function (error: application.NativeScriptError) { + console.log("Application error: " + error.name + "; " + error.message + "; " + error.nativeError); +} +application.start(); +``` \ No newline at end of file diff --git a/application.md b/application.md deleted file mode 100644 index 190774502..000000000 --- a/application.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -nav-title: "NativeScript Application" -title: "Application" -description: "NativeScript Documentation: Application" -position: 2 ---- - -# Application - -Application module in NativeScript framework is the main module that must be required in order to have working application. This module provides several callback functions to notify developer when specific event occurred. - -For example you can set callback for: - -+ onLaunch(context) - method called when application launch. -+ onSuspend() - method called when the application is suspended. -+ onResume() - method called when the application is resumed after it has been suspended. -+ onExit() - method called when the application is about to exit. -+ onLowMemory() - method called when there is low memory on the target device. - -Application module also provides short-cut for navigating to main page - a property **mainModule**. When set application module will use the top-most frame (or will create one if there is none) and will navigate to the module with name specified in mainModule property. - -If you don't specify **mainModule** you will have to set **onLaunch** callback, instantiate **Frame** object and call its **navigate** method. -For more information see Navigation article. - -### Application start. -You are required to call **start** method on the application module once you are ready with its initialization. -This method doesn't do anything for Android application at the moment but may do in the future. -In iOS application this call will start UIApplication and will trigger its UI message loop. - -Here is sample code that specify **mainModule**, **onLowMemory** callback and **starts** application: -``` JavaScript -var application = require("application"); -application.mainModule = "mainPage"; -application.onLowMemory = function () { - // clean up some resources to free memory. -}; -application.start(); -``` diff --git a/navigation.md b/navigation.md index 7f61ed5ca..387e47c7c 100644 --- a/navigation.md +++ b/navigation.md @@ -128,6 +128,7 @@ topmost.navigate(navigationEntry); Sometimes, the page being navigated to would have to receive information about the context in which this navigation happened. The best example would be a master-details scenario where there are two pages -- the main page containing a list of some entities and a details page which provides details about a particular entity. In this case, when navigating to the details page it is mandatory to transfer some primary key or ID information about the entity the details page should show. This is done with the help of the **context** property of a NavigationEntry: ``` JavaScript function listViewItemTap(args) { + // Navigate to the details page with context set to the data item for specified index frames.topmost().navigate({ moduleName: "app/cuteness.io/details-page", context: appViewModel.redditItems.getItem(args.index) @@ -167,4 +168,4 @@ topmost.goBack(); topmost.goBack(); ``` #Alternatives -Alternatively, if you do not want to have different pages and navigate beteen them, you can have a single page with a TabView. You can define a different UI for each tab and when the user selects a certain tab he will be presented with this UI. \ No newline at end of file +Alternatively, if you do not want to have different pages and navigate beteen them, you can have a single page with a TabView. You can define a different UI for each tab and when the user selects a certain tab he will be presented with this UI.