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
46**Example**
47
48```ts
49import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
50
51class MyAbility extends UIAbility {
52  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
53    if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
54      console.log('The ability has been started by the way of startAbility.');
55    }
56  }
57}
58```
59
60## LastExitReason
61
62Enumerates 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.
63
64**System capability**: SystemCapability.Ability.AbilityRuntime.Core
65
66| Name                         | Value  | Description                                                        |
67| ----------------------------- | ---- | ------------------------------------------------------------ |
68| UNKNOWN          | 0    | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
69| 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**.|
70| 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.|
71| 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.|
72| 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.|
73| 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.|
74| 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.|
75| 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.|
76| 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.|
77
78**Example**
79
80```ts
81import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
82
83class MyAbility extends UIAbility {
84  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
85    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.APP_FREEZE) {
86      console.log('The ability has exit last because the ability was not responding.');
87    }
88    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.RESOURCE_CONTROL) {
89      console.log('The ability has exit last because the rss control, the lastExitReason is '+ launchParam.lastExitReason + ', the lastExitMessage is ' + launchParam.lastExitMessage);
90    }
91  }
92}
93```
94
95## OnContinueResult 
96
97Enumerates 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.
98
99**Atomic service API**: This API can be used in atomic services since API version 11.
100
101**System capability**: SystemCapability.Ability.AbilityRuntime.Core
102
103| Name                         | Value  | Description                                                        |
104| ----------------------------- | ---- | ------------------------------------------------------------ |
105| AGREE           | 0    | The ability continuation is accepted.|
106| 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.|
107| 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.|
108
109**Example**
110
111```ts
112import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
113
114class MyAbility extends UIAbility {
115  onContinue(wantParam: Record<string, Object>) {
116    return AbilityConstant.OnContinueResult.AGREE;
117  }
118}
119```
120
121## MemoryLevel
122
123Enumerates the memory levels. You can use it in [onMemoryLevel(level)](js-apis-app-ability-ability.md#abilityonmemorylevel) of the UIAbility to complete different operations.
124
125**Atomic service API**: This API can be used in atomic services since API version 11.
126
127**System capability**: SystemCapability.Ability.AbilityRuntime.Core
128
129| Name                        | Value| Description               |
130| ---                         | --- | ---           |
131| MEMORY_LEVEL_MODERATE       | 0   | Moderate memory usage.|
132| MEMORY_LEVEL_LOW            | 1   | Low memory usage.  |
133| MEMORY_LEVEL_CRITICAL       | 2   | High memory usage.  |
134
135**Example**
136
137```ts
138import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
139
140class MyAbility extends UIAbility {
141  onMemoryLevel(level: AbilityConstant.MemoryLevel) {
142    if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
143      console.log('The memory of device is critical, please release some memory.');
144    }
145  }
146}
147```
148
149## WindowMode<sup>12+</sup>
150
151Enumerates 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.
152
153**System capability**: SystemCapability.Ability.AbilityRuntime.Core
154
155| Name                       | Value| Description                |
156| ---                         | --- | ---                  |
157| WINDOW_MODE_FULLSCREEN      | 1   | Full screen mode. It takes effect only on 2-in-1 devices and tablets.        |
158| 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.  |
159| 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.  |
160
161**Example**
162
163```ts
164import { UIAbility, StartOptions, Want, AbilityConstant } from '@kit.AbilityKit';
165import { BusinessError } from '@kit.BasicServicesKit';
166
167let want: Want = {
168  bundleName: 'com.example.myapplication',
169  abilityName: 'EntryAbility'
170};
171let option: StartOptions = {
172  windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_PRIMARY
173};
174
175// Ensure that the context is obtained.
176class MyAbility extends UIAbility {
177  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
178    this.context.startAbility(want, option).then(() => {
179      console.log('Succeed to start ability.');
180    }).catch((error: BusinessError) => {
181      console.error(`Failed to start ability with error: ${JSON.stringify(error)}`);
182    });
183  }
184}
185```
186
187## OnSaveResult
188
189Enumerates 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.
190
191**Atomic service API**: This API can be used in atomic services since API version 11.
192
193**System capability**: SystemCapability.Ability.AbilityRuntime.Core
194
195| Name                         | Value  | Description                                                        |
196| ----------------------------- | ---- | ------------------------------------------------------------ |
197| ALL_AGREE           | 0    | Always agreed to save the status.|
198| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
199| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
200| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
201| RECOVERY_REJECT  | 4    | Rejected to restore the saved status.|
202| ALL_REJECT  | 5    | Always rejected to save the status.|
203
204**Example**
205
206```ts
207import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
208
209class MyAbility extends UIAbility {
210  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
211    return AbilityConstant.OnSaveResult.ALL_AGREE;
212  }
213}
214```
215
216## StateType
217
218Enumerates 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.
219
220**Atomic service API**: This API can be used in atomic services since API version 11.
221
222**System capability**: SystemCapability.Ability.AbilityRuntime.Core
223
224| Name                         | Value  | Description                                                        |
225| ----------------------------- | ---- | ------------------------------------------------------------ |
226| CONTINUATION           | 0    | Saving the status in continuation.|
227| APP_RECOVERY           | 1    | Saving the status in application recovery.|
228
229**Example**
230
231```ts
232import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
233
234class MyAbility extends UIAbility {
235  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
236    if (reason === AbilityConstant.StateType.CONTINUATION) {
237      console.log('Save the ability data when the ability continuation.');
238    }
239    return AbilityConstant.OnSaveResult.ALL_AGREE;
240  }
241}
242```
243
244## ContinueState<sup>10+</sup>
245
246Enumerates 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).
247
248**Atomic service API**: This API can be used in atomic services since API version 11.
249
250**System capability**: SystemCapability.Ability.AbilityRuntime.Core
251
252| Name          | Value      | Description                                                        |
253| ------------- | --------- | ------------------------------------------------------------ |
254| ACTIVE        | 0         | Mission continuation is activated for the current application.                             |
255| INACTIVE      | 1         | Mission continuation is not activated for the current application.                           |
256
257**Example**
258
259```ts
260import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
261import { BusinessError } from '@kit.BasicServicesKit';
262
263class MyAbility extends UIAbility {
264  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
265    this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
266      console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
267    });
268  }
269}
270```
271