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| launchReasonMessage<sup>18+</sup> | string | No| Yes| Detailed message that describes the ability launch reason.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 27| 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.| 28| 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.| 29| lastExitDetailInfo<sup>18+</sup> | [LastExitDetailInfo](#lastexitdetailinfo18) | No| Yes| Detailed information about the last exit.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 30 31## LaunchReason 32 33Enumerates 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. 34 35**System capability**: SystemCapability.Ability.AbilityRuntime.Core 36 37| Name | Value | Description | 38| ----------------------------- | ---- | ------------------------------------------------------------ | 39| UNKNOWN | 0 | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 40| 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.| 41| 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.| 42| 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.| 43| 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.| 44| 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.| 45| AUTO_STARTUP<sup>11+</sup> | 8 | The ability is automatically started upon system boot.| 46| 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.| 47| 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.| 48 49**Example** 50 51```ts 52import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 53 54class MyAbility extends UIAbility { 55 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 56 if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) { 57 console.log('The ability has been started by the way of startAbility.'); 58 } 59 } 60} 61``` 62 63## LastExitReason 64 65Enumerates 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. 66 67**System capability**: SystemCapability.Ability.AbilityRuntime.Core 68 69| Name | Value | Description | 70| ----------------------------- | ---- | ------------------------------------------------------------ | 71| UNKNOWN | 0 | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 72| 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**.| 73| 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.| 74| 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.| 75| 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.| 76| 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.| 77| 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.| 78| 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.| 79| 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.| 80| USER_REQUEST<sup>18+</sup> | 9 | The ability exits because of an action in the multitasking center, for example, when users swipe up or hit the one-click clean button in the multitasking view.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 81| SIGNAL<sup>18+</sup> | 10 | The ability exits because it receives a kill signal from the system.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 82 83**Example** 84 85```ts 86import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 87 88class MyAbility extends UIAbility { 89 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 90 if (launchParam.lastExitReason === AbilityConstant.LastExitReason.APP_FREEZE) { 91 console.log('The ability has exit last because the ability was not responding.'); 92 } 93 if (launchParam.lastExitReason === AbilityConstant.LastExitReason.RESOURCE_CONTROL) { 94 console.log('The ability has exit last because the rss control, the lastExitReason is '+ launchParam.lastExitReason + ', the lastExitMessage is ' + launchParam.lastExitMessage); 95 } 96 } 97} 98``` 99 100## LastExitDetailInfo<sup>18+</sup> 101 102Describes the detailed information about the last exit. 103 104**Atomic service API**: This API can be used in atomic services since API version 18. 105 106**System capability**: SystemCapability.Ability.AbilityRuntime.Core 107 108| Name| Type| Read-only| Optional| Description| 109| -------- | -------- | -------- | -------- | -------- | 110| pid | number | No| No| ID of the process where the ability is running when it exits last time.| 111| processName | string | No| No| Name of the process.| 112| uid | number | No| No| UID of the application.| 113| exitSubReason | number | No| No| Specific reason for the last exit of the ability.| 114| exitMsg | string | No| No| Reason why the process was killed.| 115| rss | number | No| No| RSS value of the process.| 116| pss | number | No| No| PSS value of the process.| 117| timestamp | number | No| No| Exact time when the ability last exits.| 118 119**Example** 120 121```ts 122import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 123 124class MyAbility extends UIAbility { 125 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 126 if (launchParam.lastExitDetailInfo) { 127 console.log('pid: ' + launchParam.lastExitDetailInfo.pid + 128 '\n processName: ' + launchParam.lastExitDetailInfo.processName + 129 '\n uid: ' + launchParam.lastExitDetailInfo.uid + 130 '\n exitSubReason: ' + launchParam.lastExitDetailInfo.exitSubReason + 131 '\n exitMsg: ' + launchParam.lastExitDetailInfo.exitMsg + 132 '\n rss: ' + launchParam.lastExitDetailInfo.rss + 133 '\n pss: ' + launchParam.lastExitDetailInfo.pss + 134 '\n timestamp: ' + launchParam.lastExitDetailInfo.timestamp 135 ); 136 } 137 } 138} 139``` 140 141## OnContinueResult 142 143Enumerates 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. 144 145**Atomic service API**: This API can be used in atomic services since API version 11. 146 147**System capability**: SystemCapability.Ability.AbilityRuntime.Core 148 149| Name | Value | Description | 150| ----------------------------- | ---- | ------------------------------------------------------------ | 151| AGREE | 0 | The ability continuation is accepted.| 152| 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.| 153| 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.| 154 155**Example** 156 157```ts 158import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 159 160class MyAbility extends UIAbility { 161 onContinue(wantParam: Record<string, Object>) { 162 return AbilityConstant.OnContinueResult.AGREE; 163 } 164} 165``` 166 167## MemoryLevel 168 169Enumerates the memory levels. You can use it in [onMemoryLevel(level)](js-apis-app-ability-ability.md#abilityonmemorylevel) of the UIAbility to complete different operations. 170 171**Atomic service API**: This API can be used in atomic services since API version 11. 172 173**System capability**: SystemCapability.Ability.AbilityRuntime.Core 174 175| Name | Value| Description | 176| --- | --- | --- | 177| MEMORY_LEVEL_MODERATE | 0 | Moderate memory usage.| 178| MEMORY_LEVEL_LOW | 1 | Low memory usage. | 179| MEMORY_LEVEL_CRITICAL | 2 | High memory usage. | 180 181**Example** 182 183```ts 184import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 185 186class MyAbility extends UIAbility { 187 onMemoryLevel(level: AbilityConstant.MemoryLevel) { 188 if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) { 189 console.log('The memory of device is critical, please release some memory.'); 190 } 191 } 192} 193``` 194 195## WindowMode<sup>12+</sup> 196 197Enumerates 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. 198 199**System capability**: SystemCapability.Ability.AbilityRuntime.Core 200 201| Name | Value| Description | 202| --- | --- | --- | 203| WINDOW_MODE_FULLSCREEN | 1 | Full screen mode. It takes effect only on 2-in-1 devices and tablets. | 204| 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. | 205| 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. | 206 207**Example** 208 209```ts 210import { UIAbility, StartOptions, Want, AbilityConstant } from '@kit.AbilityKit'; 211import { BusinessError } from '@kit.BasicServicesKit'; 212 213let want: Want = { 214 bundleName: 'com.example.myapplication', 215 abilityName: 'EntryAbility' 216}; 217let option: StartOptions = { 218 windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_PRIMARY 219}; 220 221// Ensure that the context is obtained. 222class MyAbility extends UIAbility { 223 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 224 this.context.startAbility(want, option).then(() => { 225 console.log('Succeed to start ability.'); 226 }).catch((error: BusinessError) => { 227 console.error(`Failed to start ability with error: ${JSON.stringify(error)}`); 228 }); 229 } 230} 231``` 232 233## OnSaveResult 234 235Enumerates 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. 236 237**Atomic service API**: This API can be used in atomic services since API version 11. 238 239**System capability**: SystemCapability.Ability.AbilityRuntime.Core 240 241| Name | Value | Description | 242| ----------------------------- | ---- | ------------------------------------------------------------ | 243| ALL_AGREE | 0 | Always agreed to save the status.| 244| CONTINUATION_REJECT | 1 | Rejected to save the status in continuation.| 245| CONTINUATION_MISMATCH | 2 | Continuation mismatch.| 246| RECOVERY_AGREE | 3 | Agreed to restore the saved status.| 247| RECOVERY_REJECT | 4 | Rejected to restore the saved status.| 248| ALL_REJECT | 5 | Always rejected to save the status.| 249 250**Example** 251 252```ts 253import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 254 255class MyAbility extends UIAbility { 256 onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) { 257 return AbilityConstant.OnSaveResult.ALL_AGREE; 258 } 259} 260``` 261 262## StateType 263 264Enumerates 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. 265 266**Atomic service API**: This API can be used in atomic services since API version 11. 267 268**System capability**: SystemCapability.Ability.AbilityRuntime.Core 269 270| Name | Value | Description | 271| ----------------------------- | ---- | ------------------------------------------------------------ | 272| CONTINUATION | 0 | Saving the status in continuation.| 273| APP_RECOVERY | 1 | Saving the status in application recovery.| 274 275**Example** 276 277```ts 278import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 279 280class MyAbility extends UIAbility { 281 onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) { 282 if (reason === AbilityConstant.StateType.CONTINUATION) { 283 console.log('Save the ability data when the ability continuation.'); 284 } 285 return AbilityConstant.OnSaveResult.ALL_AGREE; 286 } 287} 288``` 289 290## ContinueState<sup>10+</sup> 291 292Enumerates 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). 293 294**Atomic service API**: This API can be used in atomic services since API version 11. 295 296**System capability**: SystemCapability.Ability.AbilityRuntime.Core 297 298| Name | Value | Description | 299| ------------- | --------- | ------------------------------------------------------------ | 300| ACTIVE | 0 | Mission continuation is activated for the current application. | 301| INACTIVE | 1 | Mission continuation is not activated for the current application. | 302 303**Example** 304 305```ts 306import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 307import { BusinessError } from '@kit.BasicServicesKit'; 308 309class MyAbility extends UIAbility { 310 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 311 this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => { 312 console.info(`setMissionContinueState: ${JSON.stringify(result)}`); 313 }); 314 } 315} 316``` 317 318## CollaborateResult<sup>18+</sup> 319 320Enumerates the collaboration request results. This enum is used in multi-device collaboration scenarios to specify whether the target application accepts the collaboration request from the caller application. It is used in [onCollaborate(wantParam)](js-apis-app-ability-uiAbility.md#uiabilityoncollaborate18) of the UIAbility. 321 322**System capability**: SystemCapability.Ability.AbilityRuntime.Core 323 324| Name | Value | Description | 325| -------- | ---- | ---------- | 326| ACCEPT | 0 | Accepts the collaboration request.| 327| REJECT | 1 | Rejects the collaboration request.| 328 329**Example** 330 331```ts 332import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 333 334class MyAbility extends UIAbility { 335 onCollaborate(wantParam: Record<string, Object>) { 336 return AbilityConstant.CollaborateResult.ACCEPT; 337 } 338} 339``` 340 341## PrepareTermination<sup>15+</sup> 342 343Enumerates the actions triggered when an application is closed by the user. It must be used together with [onPrepareTermination](js-apis-app-ability-abilityStage.md#abilitystageonpreparetermination15) or [onPrepareTerminationAsync](js-apis-app-ability-abilityStage.md#abilitystageonprepareterminationasync15) of [AbilityStage](js-apis-app-ability-abilityStage.md). 344 345**Atomic service API**: This API can be used in atomic services since API version 15. 346 347**System capability**: SystemCapability.Ability.AbilityRuntime.Core 348 349| Name| Value| Description| 350| ------------- | --------- | ----------- | 351| TERMINATE_IMMEDIATELY | 0 | Executes the termination action immediately. This is the default behavior.| 352| CANCEL | 1 | Cancels the termination action.| 353 354**Example** 355 356```ts 357import { AbilityConstant, AbilityStage } from '@kit.AbilityKit'; 358 359class MyAbilityStage extends AbilityStage { 360 onPrepareTermination(): AbilityConstant.PrepareTermination { 361 console.info('MyAbilityStage.onPrepareTermination is called'); 362 return AbilityConstant.PrepareTermination.CANCEL; 363 } 364} 365``` 366