Application
The Application module provides abstraction over the platform-specific Application implementations. The module lets you manage the lifecycle of your NativeScript applications from starting the application to handling application events and creating platform-specific logic.
The pre-required application
module is used throughout the following code snippets.
Android Broadcast Receiver
Application Module Android Specific Properties
The application module provides a number of Android specific properties to access the Android app, context and activities.
// import { android as androidApp } from "tns-core-modules/application";
let isPaused = androidApp.paused; // e.g. false
let packageName = androidApp.packageName; // The package ID e.g. org.nativescript.nativescriptsdkexamplesng
let nativeApp = androidApp.nativeApp; // The native APplication reference
let foregroundActivity = androidApp.foregroundActivity; // The current Activity reference
let context = androidApp.context; // The current Android context
Registering a Broadcast Receiver (Android)
Registering a broadcast receiver (Android) to check the device's battery life. The example also demonstrates how to access the native Android API using tns-platform-declarations plugin.
Unregistering a Broadcast Receiver (Android)
Application Events
The application module provides cross-platform application events to handle different application states. With the provided application events the user can handle launch, resume, suspend and exit states or provide logic related to the screen orientation, uncaught errors and low memory events.
Use the application method on
to add event listeners.
Launch Event
Suspend Event
Resume Event
Exit Event
Displayed Event
Low Memory Event
Orientation Changed Event
Uncaught Error Event
Unsubscribing Event Listener
Use the application method off
to remove the registered event listeners.
Check Platform
Use the following code in case you need to check somewhere in your code the platform you are running against:
iOS Notification Observer
Application Module iOS Specific Properties
The application module provides a number of iOS specific properties to access the iOS app, delegate and root view controller, etc..
// import { ios as iosApp } from "tns-core-modules/application";
// https://developer.apple.com/documentation/uikit/uiapplicationdelegate?language=objc
let delegate = iosApp.delegate; // the iOS application delegate
let nativeApp = iosApp.nativeApp; // The native iOS app
// https://developer.apple.com/documentation/uikit/uiwindow/1621581-rootviewcontroller?language=objc
let rootController = iosApp.rootController; // the iOS rootViewController
let window = iosApp.window; // UIWindow
Adding a Notification Observer (iOS)
Use addNotificationObserver
to add iOS notifications observer
Removing a Notification Observer (iOS)
When no longer needed, remove the notification observer