8.7 KiB
8.7 KiB
nav-title | title | description | position |
---|---|---|---|
Application Management in NativeScript | App: Management | Learn how to manage the life cycle of NativeScript applications from application start to storing user-defined settings. | 4 |
Application Management
The application
module lets you manage the life cycle of your NativeScript apps from starting the application to storing user-defined settings.
Start Application
You must call the start method of the application module after the module initialization.
This method is required for iOS applications.
Example
/*
iOS calls UIApplication and triggers the application main event loop.
*/
var application = require("application");
application.mainModule = "main-page";
application.start();
/*
iOS calls UIApplication and triggers the application main event loop.
*/
import application = require("application");
application.mainModule = "main-page";
application.start();
Use Application Events
NativeScript applications have the following life cycle events.
onLaunch(context)
: This method is called when application launch.onSuspend()
: This method is called when the application is suspended.onResume()
: This method is called when the application is resumed after it has been suspended.onExit()
: This method is called when the application is about to exit.onLowMemory()
: This method is called when the memory on the target device is low.onUncaughtError(error)
: This method is called when an uncaught application error is present.
Example
var application = require("application");
application.mainModule = "main-page";
application.on(application.launchEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an android.content.Intent class.
console.log("Launched Android application with the following intent: " + args.android + ".");
} else if (args.ios !== undefined) {
// For iOS applications, args.ios is NSDictionary (launchOptions).
console.log("Launched iOS application with options: " + args.ios);
}
});
application.on(application.suspendEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.resumeEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.exitEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.lowMemoryEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.uncaughtErrorEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an NativeScriptError.
console.log("NativeScriptError: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log("NativeScriptError: " + args.ios);
}
});
application.start();
import application = require("application");
application.mainModule = "main-page";
application.on(application.launchEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android.content.Intent class.
console.log("Launched Android application with the following intent: " + args.android + ".");
} else if (args.ios !== undefined) {
// For iOS applications, args.ios is NSDictionary (launchOptions).
console.log("Launched iOS application with options: " + args.ios);
}
});
application.on(application.suspendEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.resumeEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.exitEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.lowMemoryEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android activity class.
console.log("Activity: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is UIApplication.
console.log("UIApplication: " + args.ios);
}
});
application.on(application.uncaughtErrorEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an NativeScriptError.
console.log("NativeScriptError: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log("NativeScriptError: " + args.ios);
}
});
application.start();
Persist and Restore Application Settings
To persist user-defined settings, you need to use the application-settings
module. The application-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 have two required parameters: a key and value.
Example
var applicationSettings = require("application-settings");
// Event handler for Page "loaded" event attached in main-page.xml.
function pageLoaded(args) {
applicationSettings.setString("Name", "John Doe");
console.log(applicationSettings.getString("Name")); // Prints "John Doe".
applicationSettings.setBoolean("Married", false);
console.log(applicationSettings.getBoolean("Married")); // Prints false.
applicationSettings.setNumber("Age", 42);
console.log(applicationSettings.getNumber("Age")); // Prints 42.
console.log(applicationSettings.hasKey("Name")); // Prints true.
applicationSettings.remove("Name"); // Removes the Name entry.
console.log(applicationSettings.hasKey("Name")); // Prints false.
}
exports.pageLoaded = pageLoaded;
import observable = require("data/observable");
import applicationSettings = require("application-settings");
// Event handler for Page "loaded" event attached in main-page.xml.
export function pageLoaded(args: observable.EventData) {
applicationSettings.setString("Name", "John Doe");
console.log(applicationSettings.getString("Name"));// Prints "John Doe".
applicationSettings.setBoolean("Married", false);
console.log(applicationSettings.getBoolean("Married"));// Prints false.
applicationSettings.setNumber("Age", 42);
console.log(applicationSettings.getNumber("Age"));// Prints 42.
console.log(applicationSettings.hasKey("Name"));// Prints true.
applicationSettings.remove("Name");// Removes the Name entry.
console.log(applicationSettings.hasKey("Name"));// Prints false.
}