3.NativeScript Application Life cycle And Store Data

宋典
2023-12-01

UseApplication Callbacks

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;

 

 

 类似资料: