Application Management
The application
module lets you manage the life cycle of your NativeScript apps from starting the application to storing user-defined settings.
- Application Run
- Use Application Events
- Android Activity Events
- iOS UIApplicationDelegate
- Persist and Restore Application Settings
Application Run
The method run
from the application
module is required to start the application and accepts the path to the root XML file.
Note: You must call the
run
method of the application module after the module initialization. Any code after therun
call will not be executed.
Note: Prior to version 4.0.0 all NativeScript application had single topmost
Frame
implicitly created by theapplication.start()
method. With NativeScript 4.x.x and above the rootFrame
is no longer implicitly created. Instead you can specify anyView
to be the root of your application while usingapplication.run({ moduleName: "app-root"})
whereapp-root
is the file containing your rootView
. More about theFrame
API and navigation could be found in the navigation article
Use Application Events
NativeScript applications have the following life cycle events.
-
launch
: This event is raised when application launch. -
suspend
: This event is raised when the application is suspended. -
resume
: This event is raised when the application is resumed after it has been suspended. -
displayed
: This event is raised when the UIelements are rendered. -
orientationChanged
: This event is raised when the device changes orientation. -
exit
: This event is raised when the application is about to exit. -
lowMemory
: This event is raised when the memory on the target device is low. -
uncaughtError
: This event is raised when an uncaught application error is present.
Android Activity Events
NativeScript applications have the following Android specific activity events:
-
activityCreated
: This event is raised when activity is created. -
activityDestroyed
: This event is raised when activity is destroyed. -
activityStarted
: This event is raised when activity is started. -
activityPaused
: This event is raised when activity is paused. -
activityResumed
: This event is raised when activity is resumed. -
activityStopped
: This event is raised when activity is stopped. -
saveActivityState
: This event is raised to retrieve per-instance state from an activity before being killed so that the state can be restored. -
activityResult
: This event is raised when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. -
activityBackPressed
: This event is raised when the activity has detected the user's press of the back key.
iOS UIApplicationDelegate
In NativeScript, you can specify custom UIApplicationDelegate
for the iOS application:
Note: If you’re using TypeScript in your NativeScript apps, you need to install the tns-platform-declarations plugin to add typings for native iOS APIs such as
UIApplicationDelegate
.
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.