• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> The APIs of this module can be used only in the stage model.
9
10## Modules to Import
11
12```ts
13import AbilityConstant from '@ohos.app.ability.AbilityConstant';
14```
15
16## AbilityConstant.LaunchParam
17
18Defines the parameters for starting an ability.
19
20**System capability**: SystemCapability.Ability.AbilityRuntime.Core
21
22| Name| Type| Read-only| Mandatory| Description|
23| -------- | -------- | -------- | -------- | -------- |
24| launchReason | [LaunchReason](#abilityconstantlaunchreason)| No| Yes| Ability launch reason, which is an enumerated type.|
25| lastExitReason | [LastExitReason](#abilityconstantlastexitreason) | No| Yes| Reason for the last exit, which is an enumerated type.|
26
27## AbilityConstant.LaunchReason
28
29Enumerates 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.
30
31**System capability**: SystemCapability.Ability.AbilityRuntime.Core
32
33| Name                         | Value  | Description                                                        |
34| ----------------------------- | ---- | ------------------------------------------------------------ |
35| UNKNOWN          | 0    | Unknown reason.|
36| START_ABILITY          | 1    | The ability is started by calling [startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability).|
37| CALL | 2    | The ability is started by calling [startAbilityByCall](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilitybycall).|
38| CONTINUATION           | 3    | The ability is started by means of cross-device migration.|
39| APP_RECOVERY           | 4    | The ability is automatically started when the application is restored from a fault.|
40| SHARE<sup>10+</sup>           | 5    | The ability is started by means of atomic service sharing.|
41| AUTO_STARTUP<sup>11+</sup>           | 8    | The ability is automatically started upon system boot.|
42| INSIGHT_INTENT<sup>11+</sup>           | 9    | The ability is started by the InsightIntent framework.|
43
44**Example**
45
46```ts
47import UIAbility from '@ohos.app.ability.UIAbility';
48import Want from '@ohos.app.ability.Want';
49import AbilityConstant from '@ohos.app.ability.AbilityConstant';
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## AbilityConstant.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.|
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.|
71| CPP_CRASH<sup>10+</sup>  | 3    | The ability exits due to abnormal signals on the local host.|
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.|
73| APP_FREEZE<sup>10+</sup>  | 5    | The ability exits because watchdog detects that the application is frozen.|
74| PERFORMANCE_CONTROL<sup>10+</sup>  | 6    | The ability exits due to system performance problems, for example, insufficient device memory.|
75| RESOURCE_CONTROL<sup>10+</sup>  | 7    | The ability exits because the system resource usage (CPU, I/O, or memory usage) exceeds the upper limit.|
76| UPGRADE<sup>10+</sup>  | 8    | The ability exits due to an update.|
77
78**Example**
79
80```ts
81import UIAbility from '@ohos.app.ability.UIAbility';
82import Want from '@ohos.app.ability.Want';
83import AbilityConstant from '@ohos.app.ability.AbilityConstant';
84
85class MyAbility extends UIAbility {
86    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
87        if (launchParam.lastExitReason === AbilityConstant.LastExitReason.APP_FREEZE) {
88            console.log('The ability has exit last because the ability was not responding.');
89        }
90    }
91}
92```
93
94## AbilityConstant.OnContinueResult
95
96Enumerates 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.
97
98**System capability**: SystemCapability.Ability.AbilityRuntime.Core
99
100| Name                         | Value  | Description                                                        |
101| ----------------------------- | ---- | ------------------------------------------------------------ |
102| AGREE           | 0    | The ability continuation is accepted.|
103| 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.|
104| 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.|
105
106**Example**
107
108```ts
109import UIAbility from '@ohos.app.ability.UIAbility';
110import AbilityConstant from '@ohos.app.ability.AbilityConstant';
111
112class MyAbility extends UIAbility {
113    onContinue(wantParam: Record<string, Object>) {
114        return AbilityConstant.OnContinueResult.AGREE;
115    }
116}
117```
118
119## AbilityConstant.WindowMode
120
121Enumerates the window modes in which an ability can be displayed at startup. It can be used in **startAbility()** to specify the window mode when the ability is started.
122
123**System capability**: SystemCapability.Ability.AbilityRuntime.Core
124
125**System API**: This is a system API and cannot be called by third-party applications.
126
127| Name                       | Value| Description                |
128| ---                         | --- | ---                  |
129| WINDOW_MODE_UNDEFINED       | 0   | Undefined window mode.      |
130| WINDOW_MODE_FULLSCREEN      | 1   | The ability is displayed in full screen.           |
131| WINDOW_MODE_SPLIT_PRIMARY   | 100 | The left screen in horizontal direction or the upper screen in vertical direction is the primary window.  |
132| WINDOW_MODE_SPLIT_SECONDARY | 101 | The right screen in horizontal direction or the lower screen in vertical direction is the secondary window.  |
133| WINDOW_MODE_FLOATING        | 102 | The ability is displayed in a floating window.|
134
135**Example**
136
137```ts
138import UIAbility from '@ohos.app.ability.UIAbility';
139import StartOptions from '@ohos.app.ability.StartOptions';
140import Want from '@ohos.app.ability.Want';
141import { BusinessError } from '@ohos.base';
142import AbilityConstant from '@ohos.app.ability.AbilityConstant';
143
144let want: Want = {
145  bundleName: 'com.example.myapplication',
146  abilityName: 'EntryAbility'
147};
148let option: StartOptions = {
149  windowMode: AbilityConstant.WindowMode.WINDOW_MODE_FULLSCREEN
150};
151
152// Ensure that the context is obtained.
153class MyAbility extends UIAbility {
154  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
155    this.context.startAbility(want, option).then(()=>{
156      console.log('Succeed to start ability.');
157    }).catch((error: BusinessError)=>{
158      console.error('Failed to start ability with error: ${JSON.stringify(error)}');
159    });
160  }
161}
162```
163
164## AbilityConstant.MemoryLevel
165
166Enumerates the memory levels. You can use it in [onMemoryLevel(level)](js-apis-app-ability-ability.md#abilityonmemorylevel) of the UIAbility to complete different operations.
167
168**System capability**: SystemCapability.Ability.AbilityRuntime.Core
169
170| Name                        | Value| Description               |
171| ---                         | --- | ---           |
172| MEMORY_LEVEL_MODERATE       | 0   | Moderate memory usage.|
173| MEMORY_LEVEL_LOW            | 1   | Low memory usage.  |
174| MEMORY_LEVEL_CRITICAL       | 2   | High memory usage.  |
175
176**Example**
177
178```ts
179import UIAbility from '@ohos.app.ability.UIAbility';
180import AbilityConstant from '@ohos.app.ability.AbilityConstant';
181
182class MyAbility extends UIAbility {
183    onMemoryLevel(level: AbilityConstant.MemoryLevel) {
184        if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
185            console.log('The memory of device is critical, please release some memory.');
186        }
187    }
188}
189```
190
191## AbilityConstant.OnSaveResult
192
193Enumerates 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.
194
195**System capability**: SystemCapability.Ability.AbilityRuntime.Core
196
197| Name                         | Value  | Description                                                        |
198| ----------------------------- | ---- | ------------------------------------------------------------ |
199| ALL_AGREE           | 0    | Always agreed to save the status.|
200| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
201| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
202| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
203| RECOVERY_REJECT  | 4    | Rejected to restore the saved state.|
204| ALL_REJECT  | 5    | Always rejected to save the status.|
205
206**Example**
207
208```ts
209import UIAbility from '@ohos.app.ability.UIAbility';
210import AbilityConstant from '@ohos.app.ability.AbilityConstant';
211
212class MyAbility extends UIAbility {
213    onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
214        return AbilityConstant.OnSaveResult.ALL_AGREE;
215    }
216}
217```
218
219## AbilityConstant.StateType
220
221Enumerates 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.
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 from '@ohos.app.ability.UIAbility';
234import AbilityConstant from '@ohos.app.ability.AbilityConstant';
235
236class MyAbility extends UIAbility {
237    onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
238        if (reason === AbilityConstant.StateType.CONTINUATION) {
239            console.log('Save the ability data when the ability continuation.');
240        }
241        return AbilityConstant.OnSaveResult.ALL_AGREE;
242    }
243}
244```
245
246## AbilityConstant.ContinueState<sup>10+</sup>
247
248Enumerates 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).
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 from '@ohos.app.ability.UIAbility';
261import Want from '@ohos.app.ability.Want';
262import { BusinessError } from '@ohos.base';
263import AbilityConstant from '@ohos.app.ability.AbilityConstant';
264
265class MyAbility extends UIAbility {
266  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
267    this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
268      console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
269    });
270  }
271}
272```
273