• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.AbilityConstant (Ability-related Constants)
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: Ability-->
5<!--Owner: @littlejerry1-->
6<!--Designer: @ccllee1-->
7<!--Tester: @lixueqing513-->
8<!--Adviser: @huipeizi-->
9
10AbilityConstant provides enums related to abilities, including application launch reasons ([LaunchReason](#launchreason)), last exit reasons ([LastExitReason](#lastexitreason)), and migration results ([OnContinueResult](#oncontinueresult)).
11
12> **NOTE**
13>
14> - 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.
15>
16> - The APIs of this module can be used only in the stage model.
17
18## Modules to Import
19
20```ts
21import { AbilityConstant } from '@kit.AbilityKit';
22```
23
24## Constants
25
26**System capability**: SystemCapability.Ability.AbilityBase
27
28**Atomic service API**: This API can be used in atomic services since API version 20.
29
30| Name| Type| Value| Description|
31| ---- | -----| ---- | ---------------------------------------------------------- |
32| REASON_MESSAGE_DESKTOP_SHORTCUT<sup>20+</sup>  | string | "ReasonMessage_DesktopShortcut" | The UIAbility is launched via a home screen shortcut. If this string is obtained from the **launchReasonMessage** property in [LaunchParam](#launchparam), the UIAbility is initiated by touching a shortcut on the home screen.|
33
34## LaunchParam
35
36Describes the launch parameters, which mainly include the ability launch reasons and reasons for the last exit. The parameter values are automatically passed in by the system when the ability is launched. You do not need to change the values.
37
38**System capability**: SystemCapability.Ability.AbilityRuntime.Core
39
40| Name| Type| Read-only| Optional| Description|
41| -------- | -------- | -------- | -------- | -------- |
42| launchReason | [LaunchReason](#launchreason)| No| No| An enumerated value indicating the reason for ability launch (for example, recovery from a fault, intent invocation, or atomic service sharing). For details, see [LaunchReason](#launchreason).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
43| launchReasonMessage<sup>18+</sup> | string | No| Yes| Detailed message that describes the reason for the ability launch.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
44| lastExitReason | [LastExitReason](#lastexitreason) | No| No| An enumerated value indicating the reason for the last exit of the ability.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
45| lastExitMessage<sup>12+</sup> | string | No| No| Detailed message that describes the reason for the last exit of the ability.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
46| lastExitDetailInfo<sup>18+</sup> | [LastExitDetailInfo](#lastexitdetailinfo18) | No| Yes| Key runtime information for the last exit of the ability (including process ID, exit timestamp, and RSS memory value).<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
47
48## LaunchReason
49
50Enumerates the ability launch reasons. You can use it together with the value of **launchParam.launchReason** in [onCreate(want, launchParam)](js-apis-app-ability-uiAbility.md#oncreate) of the UIAbility to complete different operations.
51
52**System capability**: SystemCapability.Ability.AbilityRuntime.Core
53
54| Name                         | Value  | Description                                                        |
55| ----------------------------- | ---- | ------------------------------------------------------------ |
56| UNKNOWN          | 0    | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
57| START_ABILITY          | 1    | The ability is started by calling [startAbility](js-apis-inner-application-uiAbilityContext.md#startability).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
58| CALL | 2    | The ability is started by calling [startAbilityByCall](js-apis-inner-application-uiAbilityContext.md#startabilitybycall).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
59| 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.|
60| 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.|
61| 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.|
62| AUTO_STARTUP<sup>11+</sup>           | 8    | The ability is automatically started upon system boot.|
63| 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.|
64| 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.|
65| PRELOAD<sup>20+</sup>           | 11    | The ability is started through preloading.<br>**Atomic service API**: This API can be used in atomic services since API version 20.|
66
67**Example**
68
69```ts
70import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
71
72export default class MyAbility extends UIAbility {
73  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
74    if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
75      console.log('The ability has been started by the way of startAbility.');
76    }
77  }
78}
79```
80
81## LastExitReason
82
83Enumerates the reasons for the last exit of the ability. You can use it together with the value of **launchParam.lastExitReason** in [onCreate()](js-apis-app-ability-uiAbility.md#oncreate) of the UIAbility to complete different operations.
84
85**System capability**: SystemCapability.Ability.AbilityRuntime.Core
86
87| Name                         | Value  | Description                                                        |
88| ----------------------------- | ---- | ------------------------------------------------------------ |
89| UNKNOWN          | 0    | Unknown reason.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
90| ABILITY_NOT_RESPONDING<sup>(deprecated)</sup> | 1    | The ability does not respond.<br>Note: This enum is supported since API version 9 and deprecated since API version 10. You are advised to use **APP_FREEZE**.|
91| 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.<br>Note: If the application process is forcibly terminated using methods not provided by Ability Kit, such as calling [process.exit()](../apis-arkts/js-apis-process.md#processexitdeprecated) or using the kernel **kill** command, the reason for the last exit is also reported as **NORMAL**.|
92| CPP_CRASH<sup>10+</sup>  | 3    | The ability exits due to [process crash](../../dfx/cppcrash-guidelines.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
93| 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.|
94| APP_FREEZE<sup>10+</sup>  | 5    | The ability exits due to [application freeze](../../dfx/appfreeze-guidelines.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
95| 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.|
96| 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.|
97| UPGRADE<sup>10+</sup>  | 8    | The application exits due to an upgrade.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
98| USER_REQUEST<sup>18+</sup>  | 9    | The ability exits because it receives a request from the multitasking center.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
99| 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.|
100
101**Example**
102
103```ts
104import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
105
106export default class MyAbility extends UIAbility {
107  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
108    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.APP_FREEZE) {
109      console.log('The ability has exit last because the ability was not responding.');
110    }
111    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.RESOURCE_CONTROL) {
112      console.log('The ability has exited last because the rss control, the lastExitReason is '+ launchParam.lastExitReason + ', the lastExitMessage is ' + launchParam.lastExitMessage);
113    }
114  }
115}
116```
117
118## LastExitDetailInfo<sup>18+</sup>
119
120Describes the key runtime information of the process where the ability last exited.
121
122**System capability**: SystemCapability.Ability.AbilityRuntime.Core
123
124| Name| Type| Read-only| Optional| Description|
125| -------- | -------- | -------- | -------- | -------- |
126| pid | number | No| No| ID of the process where the ability last exited.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
127| processName | string | No| No| Name of the process.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
128| uid | number | No| No| UID of the application.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
129| exitSubReason | number | No| No| Specific reason for the last exit of the ability.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
130| exitMsg | string | No| No| Reason why the process was killed.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
131| rss | number | No| No| Actual memory usage of the process, in KB.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
132| pss | number | No| No| Actual physical memory usage of the process, in KB.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
133| timestamp | number | No| No| Exact time when the ability last exited.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
134| processState<sup>20+</sup> | [appManager.ProcessState](js-apis-app-ability-appManager.md#processstate10) | No| Yes| Process status of the ability when it last exited.<br>**Atomic service API**: This API can be used in atomic services since API version 20.|
135
136**Example**
137
138```ts
139import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
140
141export default class MyAbility extends UIAbility {
142  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
143    if (launchParam.lastExitDetailInfo) {
144      console.log('pid: ' + launchParam.lastExitDetailInfo.pid +
145        '\n processName: ' + launchParam.lastExitDetailInfo.processName +
146        '\n uid: ' + launchParam.lastExitDetailInfo.uid +
147        '\n exitSubReason: ' + launchParam.lastExitDetailInfo.exitSubReason +
148        '\n exitMsg: ' + launchParam.lastExitDetailInfo.exitMsg +
149        '\n rss: ' + launchParam.lastExitDetailInfo.rss +
150        '\n pss: ' + launchParam.lastExitDetailInfo.pss +
151        '\n timestamp: ' + launchParam.lastExitDetailInfo.timestamp +
152        '\n processState: ' + launchParam.lastExitDetailInfo.processState
153      );
154    }
155  }
156}
157```
158
159## OnContinueResult
160
161Enumerates the ability continuation results. You can use it in [onContinue()](js-apis-app-ability-uiAbility.md#oncontinue) of the UIAbility to complete different operations.
162
163**Atomic service API**: This API can be used in atomic services since API version 11.
164
165**System capability**: SystemCapability.Ability.AbilityRuntime.Core
166
167| Name                         | Value  | Description                                                        |
168| ----------------------------- | ---- | ------------------------------------------------------------ |
169| AGREE           | 0    | The ability continuation is accepted.|
170| REJECT           | 1    | The ability continuation is rejected. If the application is abnormal in [onContinue](js-apis-app-ability-uiAbility.md#oncontinue), which results in abnormal display during data restoration, this result is returned.|
171| 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#oncontinue). If the ability continuation cannot be performed due to version mismatch, this result is returned.|
172
173**Example**
174
175```ts
176import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
177
178export default class MyAbility extends UIAbility {
179  onContinue(wantParam: Record<string, Object>) {
180    return AbilityConstant.OnContinueResult.AGREE;
181  }
182}
183```
184
185## MemoryLevel
186
187Enumerates the memory levels of the entire device. You can use it in [onMemoryLevel()](js-apis-app-ability-ability.md#abilityonmemorylevel) of the UIAbility to complete different operations.
188
189**Atomic service API**: This API can be used in atomic services since API version 11.
190
191**System capability**: SystemCapability.Ability.AbilityRuntime.Core
192
193| Name                        | Value| Description               |
194| ---                         | --- | ---           |
195| MEMORY_LEVEL_MODERATE       | 0   | The available memory of the entire device is moderate.|
196| MEMORY_LEVEL_LOW            | 1   | The available memory of the entire device is low.  |
197| MEMORY_LEVEL_CRITICAL       | 2   | The available memory of the entire device is critically low.  |
198
199> **NOTE**
200>
201> The trigger conditions may differ across various devices. For example, on a standard device with 12 GB of memory:
202> - When the available memory of the entire device drops to 1700 MB to 1800 MB, the **onMemoryLevel** callback with a value of **0** is triggered, indicating that the available memory is moderate.
203> - When the available memory of the entire device drops to 1600 MB to 1700 MB, the **onMemoryLevel** callback with a value of **1** is triggered, indicating that the available memory is low.
204> - When the available memory of the entire device drops below 1600 MB, the **onMemoryLevel** callback with a value of **2** is triggered, indicating that the available memory is critically low.
205
206**Example**
207
208```ts
209import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
210
211export default class MyAbility extends UIAbility {
212  onMemoryLevel(level: AbilityConstant.MemoryLevel) {
213    if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
214      console.log('The memory of device is critical, please release some memory.');
215    }
216  }
217}
218```
219
220## WindowMode<sup>12+</sup>
221
222Enumerates the window modes in which a UIAbility can be displayed at startup. You can use it in [startAbility](js-apis-inner-application-uiAbilityContext.md#startability).
223
224**System capability**: SystemCapability.Ability.AbilityRuntime.Core
225
226| Name                       | Value| Description                |
227| ---                         | --- | ---                  |
228| WINDOW_MODE_FULLSCREEN      | 1   | Full screen mode. It takes effect only on 2-in-1 devices and tablets. |
229| 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.  |
230| 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.  |
231
232**Example**
233
234```ts
235import { UIAbility, StartOptions, Want, AbilityConstant } from '@kit.AbilityKit';
236import { BusinessError } from '@kit.BasicServicesKit';
237
238let want: Want = {
239  bundleName: 'com.example.myapplication',
240  abilityName: 'EntryAbility'
241};
242let option: StartOptions = {
243  windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_PRIMARY
244};
245
246// Ensure that the context is obtained.
247export default class MyAbility extends UIAbility {
248  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
249    this.context.startAbility(want, option).then(() => {
250      console.log('Succeed to start ability.');
251    }).catch((error: BusinessError) => {
252      console.error(`Failed to start ability with error: ${JSON.stringify(error)}`);
253    });
254  }
255}
256```
257
258## OnSaveResult
259
260Enumerates the result types for the operation of saving application data. You can use it in [onSaveState()](js-apis-app-ability-uiAbility.md#onsavestate) of the UIAbility to complete [UIAbility backup and restore](../../application-models/ability-recover-guideline.md).
261
262**Atomic service API**: This API can be used in atomic services since API version 11.
263
264**System capability**: SystemCapability.Ability.AbilityRuntime.Core
265
266| Name                         | Value  | Description                                                        |
267| ----------------------------- | ---- | ------------------------------------------------------------ |
268| ALL_AGREE           | 0    | Always agreed to save the status.|
269| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
270| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
271| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
272| RECOVERY_REJECT  | 4    | Rejected to restore the saved status.|
273| ALL_REJECT  | 5    | Always rejected to save the status.|
274
275**Example**
276
277```ts
278import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
279
280export default class MyAbility extends UIAbility {
281  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
282    return AbilityConstant.OnSaveResult.ALL_AGREE;
283  }
284}
285```
286
287## StateType
288
289Enumerates the scenarios for saving application data. You can use it in [onSaveState()](js-apis-app-ability-uiAbility.md#onsavestate) of the UIAbility to complete [UIAbility backup and restore](../../application-models/ability-recover-guideline.md).
290
291**Atomic service API**: This API can be used in atomic services since API version 11.
292
293**System capability**: SystemCapability.Ability.AbilityRuntime.Core
294
295| Name                         | Value  | Description                                                        |
296| ----------------------------- | ---- | ------------------------------------------------------------ |
297| CONTINUATION           | 0    | Application migration scenario.|
298| APP_RECOVERY           | 1    | Application recovery scenario.|
299
300**Example**
301
302```ts
303import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
304
305export default class MyAbility extends UIAbility {
306  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
307    if (reason === AbilityConstant.StateType.CONTINUATION) {
308      console.log('Save the ability data when the ability continuation.');
309    }
310    return AbilityConstant.OnSaveResult.ALL_AGREE;
311  }
312}
313```
314
315## ContinueState<sup>10+</sup>
316
317Enumerates the mission continuation states of the application. It is used in the [setMissionContinueState](js-apis-inner-application-uiAbilityContext.md#setmissioncontinuestate10) API of [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md).
318
319**Atomic service API**: This API can be used in atomic services since API version 11.
320
321**System capability**: SystemCapability.Ability.AbilityRuntime.Core
322
323| Name          | Value      | Description                                                        |
324| ------------- | --------- | ------------------------------------------------------------ |
325| ACTIVE        | 0         | Mission continuation is activated for the current application.                             |
326| INACTIVE      | 1         | Mission continuation is not activated for the current application.                           |
327
328**Example**
329
330```ts
331import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
332import { BusinessError } from '@kit.BasicServicesKit';
333
334export default class MyAbility extends UIAbility {
335  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
336    this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
337      console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
338    });
339  }
340}
341```
342
343## CollaborateResult<sup>18+</sup>
344
345Enumerates the collaboration request results. You can use it in multi-device collaboration scenarios to specify whether the target application accepts the collaboration request from the caller application. You can use it in [onCollaborate()](js-apis-app-ability-uiAbility.md#oncollaborate18) of the UIAbility.
346
347**System capability**: SystemCapability.Ability.AbilityRuntime.Core
348
349| Name    | Value  | Description      |
350| -------- | ---- | ---------- |
351| ACCEPT   | 0    | Accepts the collaboration request.|
352| REJECT   | 1    | Rejects the collaboration request.|
353
354**Example**
355
356```ts
357import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
358
359export default class MyAbility extends UIAbility {
360  onCollaborate(wantParam: Record<string, Object>) {
361    return AbilityConstant.CollaborateResult.ACCEPT;
362  }
363}
364```
365
366## PrepareTermination<sup>15+</sup>
367
368Enumerates the actions triggered when an application is closed by the user. You can use it in [onPrepareTermination](js-apis-app-ability-abilityStage.md#onpreparetermination15) or [onPrepareTerminationAsync](js-apis-app-ability-abilityStage.md#onprepareterminationasync15) of [AbilityStage](js-apis-app-ability-abilityStage.md).
369
370**Atomic service API**: This API can be used in atomic services since API version 15.
371
372**System capability**: SystemCapability.Ability.AbilityRuntime.Core
373
374| Name| Value| Description|
375| ------------- | --------- | ----------- |
376| TERMINATE_IMMEDIATELY | 0 | Executes the termination action immediately. This is the default behavior.|
377| CANCEL | 1 | Cancels the termination action.|
378
379**Example**
380
381```ts
382import { AbilityConstant, AbilityStage } from '@kit.AbilityKit';
383
384export default class MyAbilityStage extends AbilityStage {
385  onPrepareTermination(): AbilityConstant.PrepareTermination {
386    console.info('MyAbilityStage.onPrepareTermination is called');
387    return AbilityConstant.PrepareTermination.CANCEL;
388  }
389}
390```
391