0
# Application Management
1
2
Core application lifecycle, navigation, and configuration management for NativeScript applications. This module provides essential functionality for app startup, event handling, and platform-specific application management.
3
4
## Capabilities
5
6
### Application Namespace
7
8
Main application management interface providing lifecycle control and system integration.
9
10
```typescript { .api }
11
/**
12
* Main application management namespace
13
*/
14
namespace Application {
15
// Event constants
16
const launchEvent: "launch";
17
const displayedEvent: "displayed";
18
const suspendEvent: "suspend";
19
const resumeEvent: "resume";
20
const exitEvent: "exit";
21
const lowMemoryEvent: "lowMemory";
22
const orientationChangedEvent: "orientationChanged";
23
const uncaughtErrorEvent: "uncaughtError";
24
25
// Core application functions
26
function run(entry?: any): void;
27
function getMainEntry(): any;
28
function getRootView(): View;
29
function orientation(): "portrait" | "landscape" | "unknown";
30
function hasLaunched(): boolean;
31
function getNativeApplication(): any;
32
33
// Resource and CSS management
34
function setResources(resources: any): void;
35
function setCssFileName(cssFileName: string): void;
36
function getCssFileName(): string;
37
function loadAppCss(): void;
38
function addCss(cssText: string): void;
39
40
// Event handling
41
function on(eventName: string, callback: Function): void;
42
function off(eventName: string, callback?: Function): void;
43
44
// Platform-specific instances
45
const android: AndroidApplication;
46
const ios: iOSApplication;
47
}
48
```
49
50
**Usage Examples:**
51
52
```typescript
53
import { Application } from "tns-core-modules";
54
55
// Application startup
56
Application.run({ moduleName: "main-page" });
57
58
// Listen to application events
59
Application.on(Application.launchEvent, (args) => {
60
console.log("App launched");
61
});
62
63
Application.on(Application.suspendEvent, () => {
64
console.log("App suspended");
65
});
66
67
// Check application state
68
if (Application.hasLaunched()) {
69
console.log("App has already launched");
70
}
71
72
// Get current orientation
73
const orientation = Application.orientation();
74
console.log("Current orientation:", orientation);
75
```
76
77
### Application Event Interfaces
78
79
Event data interfaces for application lifecycle events.
80
81
```typescript { .api }
82
interface ApplicationEventData {
83
eventName: string;
84
object: any;
85
}
86
87
interface LaunchEventData extends ApplicationEventData {
88
android?: any;
89
ios?: any;
90
}
91
92
interface OrientationChangedEventData extends ApplicationEventData {
93
newValue: string;
94
oldValue: string;
95
}
96
97
interface UnhandledErrorEventData extends ApplicationEventData {
98
android?: any;
99
ios?: any;
100
error: Error;
101
}
102
103
interface CssChangedEventData extends ApplicationEventData {
104
cssFile?: string;
105
cssText?: string;
106
}
107
```
108
109
### Android Application Interface
110
111
Android-specific application management interface.
112
113
```typescript { .api }
114
interface AndroidApplication {
115
context: any; // Android Context
116
currentContext: any;
117
foregroundActivity: any;
118
startActivity: any;
119
packageName: string;
120
hasActionBar: boolean;
121
122
// Event handling
123
on(eventName: string, callback: Function): void;
124
off(eventName: string, callback?: Function): void;
125
}
126
127
// Android-specific event data interfaces
128
interface AndroidActivityEventData extends ApplicationEventData {
129
activity: any;
130
intent: any;
131
}
132
133
interface AndroidActivityRequestPermissionsEventData extends AndroidActivityEventData {
134
requestCode: number;
135
permissions: string[];
136
grantResults: number[];
137
}
138
139
interface AndroidActivityResultEventData extends AndroidActivityEventData {
140
requestCode: number;
141
resultCode: number;
142
intent: any;
143
}
144
```
145
146
### iOS Application Interface
147
148
iOS-specific application management interface.
149
150
```typescript { .api }
151
interface iOSApplication {
152
delegate: any; // UIApplicationDelegate
153
rootController: any;
154
window: any;
155
156
// Event handling
157
on(eventName: string, callback: Function): void;
158
off(eventName: string, callback?: Function): void;
159
}
160
```
161
162
### Application Settings
163
164
Persistent key-value storage for application configuration and user preferences.
165
166
```typescript { .api }
167
/**
168
* Application settings namespace for persistent storage
169
*/
170
namespace ApplicationSettings {
171
// String operations
172
function setString(key: string, value: string): void;
173
function getString(key: string, defaultValue?: string): string;
174
175
// Boolean operations
176
function setBoolean(key: string, value: boolean): void;
177
function getBoolean(key: string, defaultValue?: boolean): boolean;
178
179
// Numeric operations
180
function setNumber(key: string, value: number): void;
181
function getNumber(key: string, defaultValue?: number): number;
182
183
// General operations
184
function hasKey(key: string): boolean;
185
function remove(key: string): void;
186
function clear(): void;
187
function flush(): void;
188
function getAllKeys(): string[];
189
}
190
```
191
192
**Usage Examples:**
193
194
```typescript
195
import { ApplicationSettings } from "tns-core-modules";
196
197
// Store user preferences
198
ApplicationSettings.setString("username", "john_doe");
199
ApplicationSettings.setBoolean("darkMode", true);
200
ApplicationSettings.setNumber("fontSize", 16);
201
202
// Retrieve settings with defaults
203
const username = ApplicationSettings.getString("username", "guest");
204
const isDarkMode = ApplicationSettings.getBoolean("darkMode", false);
205
const fontSize = ApplicationSettings.getNumber("fontSize", 14);
206
207
// Check if setting exists
208
if (ApplicationSettings.hasKey("username")) {
209
console.log("Username is configured");
210
}
211
212
// Remove specific setting
213
ApplicationSettings.remove("tempData");
214
215
// Clear all settings
216
ApplicationSettings.clear();
217
```