Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions application-management.md
Original file line number Diff line number Diff line change
@@ -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();
```
38 changes: 0 additions & 38 deletions application.md

This file was deleted.

3 changes: 2 additions & 1 deletion navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
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.