NativeScript applications have thefollowing life cycle events.
- start(): Call the start method after the module initialization to start application.
//在start()之後調用,如果是點擊back鍵,重新進入則會調用onLaunch(context),然後調用onResume()
- onLaunch(context): This method is called when application launch.
//點擊home鍵或者back鍵時調用
- 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.
//當點擊back鍵時,不調用onExit(),只有直接kill掉纔會調用
- 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.
Persistand Restore Application Settings
To persist user-defined settings, you need to use the local-settings
module. The
local-settings
module is a staticsingleton hash table that stores key-value pairs for the application.
The getter methods have two parameters: a key and an optional default valueto return if the specified key does not exist. The setter methods have tworequired parameters: a key and value.
Example
- JavaScript
- TypeScript
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;