1# @ohos.app.ability.AbilityConstant (AbilityConstant)
2
3The **AbilityConstant** module defines the UIAbility-related enums, including the initial launch reasons, reasons for the last exit, ability continuation results, and window modes.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> - The APIs of this module can be used only in the stage model.
10
11## Modules to Import
12
13```ts
14import { AbilityConstant } from '@kit.AbilityKit';
15```
16
17## LaunchParam
18
19Defines the parameters for starting an ability. The parameter values are automatically passed in by the system when the ability is started. You do not need to change the values.
20
21**System capability**: SystemCapability.Ability.AbilityRuntime.Core
22
23| Name| Type| Read-only| Optional| Description|
24| -------- | -------- | -------- | -------- | -------- |
25| launchReason | [LaunchReason](#launchreason)| No| No| Ability launch reason, which is an enumerated type.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
26| lastExitReason | [LastExitReason](#lastexitreason) | No| No| Reason for the last exit, which is an enumerated type.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
27| lastExitMessage<sup>12+</sup> | string | No| No| Reason for the last exit.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
28
29## LaunchReason
30
31Enumerates the initial ability launch reasons. You can use it together with the value of **launchParam.launchReason** in [onCreate(want, launchParam)](js-apis-app-ability-uiAbility.md#uiabilityoncreate) of the UIAbility to complete different operations.
32
33**System capability**: SystemCapability.Ability.AbilityRuntime.Core
34
35| Name                         | Value  | Description                                                        |
36| ----------------------------- | ---- | ------------------------------------------------------------ |
37| UNKNOWN          | 0    | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
38| START_ABILITY          | 1    | The ability is started by calling [startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
39| CALL | 2    | The ability is started by calling [startAbilityByCall](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilitybycall).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
40| CONTINUATION           | 3    | The ability is started by means of cross-device migration.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
41| APP_RECOVERY           | 4    | The ability is automatically started when the application is restored from a fault.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
42| SHARE<sup>10+</sup>           | 5    | The ability is started by means of atomic service sharing.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
43| AUTO_STARTUP<sup>11+</sup>           | 8    | The ability is automatically started upon system boot.|
44| INSIGHT_INTENT<sup>11+</sup>           | 9    | The ability is started by the InsightIntent framework.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
45| PREPARE_CONTINUATION<sup>12+</sup>           | 10    | The ability is started in advance during cross-device migration.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
46
47**Example**
48
49```ts
50import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
51
52class MyAbility extends UIAbility {
53  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
54    if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
55      console.log('The ability has been started by the way of startAbility.');
56    }
57  }
58}
59```
60
61## LastExitReason
62
63Enumerates the reasons for the last exit. You can use it together with the value of **launchParam.lastExitReason** in [onCreate(want, launchParam)](js-apis-app-ability-uiAbility.md#uiabilityoncreate) of the UIAbility to complete different operations.
64
65**System capability**: SystemCapability.Ability.AbilityRuntime.Core
66
67| Name                         | Value  | Description                                                        |
68| ----------------------------- | ---- | ------------------------------------------------------------ |
69| UNKNOWN          | 0    | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
70| ABILITY_NOT_RESPONDING<sup>(deprecated)</sup> | 1    | The ability does not respond.<br>**NOTE**<br>This enum is supported since API version 9 and deprecated since API version 10. You are advised to use **APP_FREEZE**.|
71| NORMAL | 2    | The ability exits normally because the user closes the application.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
72| CPP_CRASH<sup>10+</sup>  | 3    | The ability exits due to abnormal signals on the local host.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
73| JS_ERROR<sup>10+</sup>  | 4    | The ability exits due to a JS_ERROR fault triggered when an application has a JS syntax error that is not captured by developers.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
74| APP_FREEZE<sup>10+</sup>  | 5    | The ability exits because watchdog detects that the application is frozen.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
75| PERFORMANCE_CONTROL<sup>10+</sup>  | 6    | The ability exits due to system performance problems, for example, insufficient device memory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.<br>**NOTE**: This API will be deprecated. You are advised to use **RESOURCE_CONTROL** instead.|
76| RESOURCE_CONTROL<sup>10+</sup>  | 7    | The ability exits due to improper use of system resources. The specific error cause can be obtained through [LaunchParam.lastExitMessage](#launchparam). The possible causes are as follows:<br> - **CPU Highload**: The CPU load is high.<br> - **CPU_EXT Highload**: A fast CPU load detection is carried out.<br> - **IO Manage Control**: An I/O management and control operation is carried out.<br> -** App Memory Deterioration**: The application memory usage exceeds the threshold.<br> - **Temperature Control**: The temperature is too high or too low.<br> - **Memory Pressure**: The system is low on memory, triggering ability exiting in ascending order of priority.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
77| UPGRADE<sup>10+</sup>  | 8    | The ability exits due to an update.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
78
79**Example**
80
81```ts
82import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
83
84class MyAbility extends UIAbility {
85  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
86    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.APP_FREEZE) {
87      console.log('The ability has exit last because the ability was not responding.');
88    }
89    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.RESOURCE_CONTROL) {
90      console.log('The ability has exit last because the rss control, the lastExitReason is '+ launchParam.lastExitReason + ', the lastExitMessage is ' + launchParam.lastExitMessage);
91    }
92  }
93}
94```
95
96## OnContinueResult
97
98Enumerates the ability continuation results. You can use it together with [onContinue(wantParam)](js-apis-app-ability-uiAbility.md#uiabilityoncontinue) of the UIAbility to complete different operations.
99
100**Atomic service API**: This API can be used in atomic services since API version 11.
101
102**System capability**: SystemCapability.Ability.AbilityRuntime.Core
103
104| Name                         | Value  | Description                                                        |
105| ----------------------------- | ---- | ------------------------------------------------------------ |
106| AGREE           | 0    | The ability continuation is accepted.|
107| REJECT           | 1    | The ability continuation is rejected. If the application is abnormal in [onContinue](js-apis-app-ability-uiAbility.md#uiabilityoncontinue), which results in abnormal display during data restoration, this error code is returned.|
108| MISMATCH  | 2    | The version does not match. The application on the initiator can obtain the version of the target application from [onContinue](js-apis-app-ability-uiAbility.md#uiabilityoncontinue). If the ability continuation cannot be performed due to version mismatch, this error code is returned.|
109
110**Example**
111
112```ts
113import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
114
115class MyAbility extends UIAbility {
116  onContinue(wantParam: Record<string, Object>) {
117    return AbilityConstant.OnContinueResult.AGREE;
118  }
119}
120```
121
122## MemoryLevel
123
124Enumerates the memory levels. You can use it in [onMemoryLevel(level)](js-apis-app-ability-ability.md#abilityonmemorylevel) of the UIAbility to complete different operations.
125
126**Atomic service API**: This API can be used in atomic services since API version 11.
127
128**System capability**: SystemCapability.Ability.AbilityRuntime.Core
129
130| Name                        | Value| Description               |
131| ---                         | --- | ---           |
132| MEMORY_LEVEL_MODERATE       | 0   | Moderate memory usage.|
133| MEMORY_LEVEL_LOW            | 1   | Low memory usage.  |
134| MEMORY_LEVEL_CRITICAL       | 2   | High memory usage.  |
135
136**Example**
137
138```ts
139import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
140
141class MyAbility extends UIAbility {
142  onMemoryLevel(level: AbilityConstant.MemoryLevel) {
143    if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
144      console.log('The memory of device is critical, please release some memory.');
145    }
146  }
147}
148```
149
150## WindowMode<sup>12+</sup>
151
152Enumerates the window mode when the ability is started. It can be used together with [startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to specify the window mode for starting the ability.
153
154**System capability**: SystemCapability.Ability.AbilityRuntime.Core
155
156| Name                       | Value| Description                |
157| ---                         | --- | ---                  |
158| WINDOW_MODE_FULLSCREEN      | 1   | Full screen mode. It takes effect only on 2-in-1 devices and tablets. |
159| WINDOW_MODE_SPLIT_PRIMARY   | 100 | Primary screen (left screen in the case of horizontal orientation) in split-screen mode. It is valid only in intra-app redirection scenarios. It takes effect only on foldable devices and tablets.  |
160| WINDOW_MODE_SPLIT_SECONDARY | 101 | Secondary screen (right screen in the case of horizontal orientation) in split-screen mode. It is valid only in intra-app redirection scenarios. It takes effect only on foldable devices and tablets.  |
161
162**Example**
163
164```ts
165import { UIAbility, StartOptions, Want, AbilityConstant } from '@kit.AbilityKit';
166import { BusinessError } from '@kit.BasicServicesKit';
167
168let want: Want = {
169  bundleName: 'com.example.myapplication',
170  abilityName: 'EntryAbility'
171};
172let option: StartOptions = {
173  windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_PRIMARY
174};
175
176// Ensure that the context is obtained.
177class MyAbility extends UIAbility {
178  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
179    this.context.startAbility(want, option).then(() => {
180      console.log('Succeed to start ability.');
181    }).catch((error: BusinessError) => {
182      console.error(`Failed to start ability with error: ${JSON.stringify(error)}`);
183    });
184  }
185}
186```
187
188## OnSaveResult
189
190Enumerates the result types for the operation of saving application data. You can use it in [onSaveState(reason, wantParam)](js-apis-app-ability-uiAbility.md#uiabilityonsavestate) of the UIAbility to complete different operations.
191
192**Atomic service API**: This API can be used in atomic services since API version 11.
193
194**System capability**: SystemCapability.Ability.AbilityRuntime.Core
195
196| Name                         | Value  | Description                                                        |
197| ----------------------------- | ---- | ------------------------------------------------------------ |
198| ALL_AGREE           | 0    | Always agreed to save the status.|
199| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
200| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
201| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
202| RECOVERY_REJECT  | 4    | Rejected to restore the saved status.|
203| ALL_REJECT  | 5    | Always rejected to save the status.|
204
205**Example**
206
207```ts
208import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
209
210class MyAbility extends UIAbility {
211  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
212    return AbilityConstant.OnSaveResult.ALL_AGREE;
213  }
214}
215```
216
217## StateType
218
219Enumerates the scenarios for saving application data. You can use it in [onSaveState(reason, wantParam)](js-apis-app-ability-uiAbility.md#uiabilityonsavestate) of the UIAbility to complete different operations.
220
221**Atomic service API**: This API can be used in atomic services since API version 11.
222
223**System capability**: SystemCapability.Ability.AbilityRuntime.Core
224
225| Name                         | Value  | Description                                                        |
226| ----------------------------- | ---- | ------------------------------------------------------------ |
227| CONTINUATION           | 0    | Saving the status in continuation.|
228| APP_RECOVERY           | 1    | Saving the status in application recovery.|
229
230**Example**
231
232```ts
233import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
234
235class MyAbility extends UIAbility {
236  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
237    if (reason === AbilityConstant.StateType.CONTINUATION) {
238      console.log('Save the ability data when the ability continuation.');
239    }
240    return AbilityConstant.OnSaveResult.ALL_AGREE;
241  }
242}
243```
244
245## ContinueState<sup>10+</sup>
246
247Enumerates the mission continuation states of the application. It is used in the [setMissionContinueState](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextsetmissioncontinuestate10) API of [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md).
248
249**Atomic service API**: This API can be used in atomic services since API version 11.
250
251**System capability**: SystemCapability.Ability.AbilityRuntime.Core
252
253| Name          | Value      | Description                                                        |
254| ------------- | --------- | ------------------------------------------------------------ |
255| ACTIVE        | 0         | Mission continuation is activated for the current application.                             |
256| INACTIVE      | 1         | Mission continuation is not activated for the current application.                           |
257
258**Example**
259
260```ts
261import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
262import { BusinessError } from '@kit.BasicServicesKit';
263
264class MyAbility extends UIAbility {
265  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
266    this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
267      console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
268    });
269  }
270}
271```
272