• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.AbilityConstant (AbilityConstant)
2
3The **AbilityConstant** module defines the ability-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## Attributes
17
18## AbilityConstant.LaunchParam
19
20Defines the parameters for starting an ability.
21
22**System capability**: SystemCapability.Ability.AbilityRuntime.Core
23
24| Name| Type| Readable| Writable| Description|
25| -------- | -------- | -------- | -------- | -------- |
26| launchReason | [LaunchReason](#abilityconstantlaunchreason)| Yes| Yes| Ability launch reason, which is an enumerated type.|
27| lastExitReason | [LastExitReason](#abilityconstantlastexitreason) | Yes| Yes| Reason for the last exit, which is an enumerated type.|
28
29## AbilityConstant.LaunchReason
30
31Enumerates the initial ability launch reasons. You can use it together with [onCreate(want, launchParam)](js-apis-app-ability-uiAbility.md#uiabilityoncreate) of [Ability](js-apis-app-ability-uiAbility.md) to complete different operations.
32
33**System capability**: SystemCapability.Ability.AbilityRuntime.Core
34
35| Name                         | Value  | Description                                                        |
36| ----------------------------- | ---- | ------------------------------------------------------------ |
37| UNKNOWN          | 0    | Unknown reason.|
38| START_ABILITY          | 1    | The ability is started by calling [startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability).|
39| CALL | 2    | The ability is started by calling [startAbilityByCall](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilitybycall).|
40| CONTINUATION           | 3    | The ability is started by means of cross-device migration.|
41| APP_RECOVERY           | 4    | The ability is automatically started when the application is restored from a fault.|
42| SHARE<sup>10+</sup>           | 5    | The ability is started by means of atomic service sharing.|
43
44**Example**
45
46```ts
47import UIAbility from '@ohos.app.ability.UIAbility';
48import Want from '@ohos.app.ability.Want';
49
50class MyAbility extends UIAbility {
51    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
52        if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
53            console.log('The ability has been started by the way of startAbility.');
54        }
55    }
56}
57```
58
59## AbilityConstant.LastExitReason
60
61Enumerates the reasons for the last exit. You can use it together with [onCreate(want, launchParam)](js-apis-app-ability-uiAbility.md#uiabilityoncreate) of [Ability](js-apis-app-ability-uiAbility.md) to complete different operations.
62
63**System capability**: SystemCapability.Ability.AbilityRuntime.Core
64
65| Name                         | Value  | Description                                                        |
66| ----------------------------- | ---- | ------------------------------------------------------------ |
67| UNKNOWN          | 0    | Unknown reason.|
68| ABILITY_NOT_RESPONDING | 1    | The ability does not respond. This enum is supported since API version 9 and deprecated since API version 10. You are advised to use **APP_FREEZE**.|
69| NORMAL | 2    | The ability exits normally because the user closes the application.|
70| CPP_CRASH<sup>10+</sup>  | 3    | The ability exits due to abnormal signals on the local host.|
71| 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.|
72| APP_FREEZE<sup>10+</sup>  | 5    | The ability exits because watchdog detects that the application is frozen.|
73| PERFORMANCE_CONTROL<sup>10+</sup>  | 6    | The ability exits due to system performance problems, for example, insufficient device memory.|
74| RESOURCE_CONTROL<sup>10+</sup>  | 7    | The ability exits because the system resource usage (CPU, I/O, or memory usage) exceeds the upper limit.|
75| UPGRADE<sup>10+</sup>  | 8    | The ability exits due to an update.|
76
77**Example**
78
79```ts
80import UIAbility from '@ohos.app.ability.UIAbility';
81import Want from '@ohos.app.ability.Want';
82
83class MyAbility extends UIAbility {
84    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
85        if (launchParam.lastExitReason === AbilityConstant.LastExitReason.ABILITY_NOT_RESPONDING) {
86            console.log('The ability has exit last because the ability was not responding.');
87        }
88    }
89}
90```
91
92## AbilityConstant.OnContinueResult
93
94Enumerates the ability continuation results. You can use it together with [onContinue(wantParam)](js-apis-app-ability-uiAbility.md#uiabilityoncontinue) of [Ability](js-apis-app-ability-uiAbility.md) to complete different operations.
95
96**System capability**: SystemCapability.Ability.AbilityRuntime.Core
97
98| Name                         | Value  | Description                                                        |
99| ----------------------------- | ---- | ------------------------------------------------------------ |
100| AGREE           | 0    | The ability continuation is accepted.|
101| REJECT           | 1    | The ability continuation is rejected. If the application is abnormal in **onContinue**, which results in abnormal display during data restoration, this error code is returned.|
102| MISMATCH  | 2    | The version does not match. The application on the initiator can obtain the version of the target application from **onContinue**. If the ability continuation cannot be performed due to version mismatch, this error code is returned.|
103
104**Example**
105
106```ts
107import UIAbility from '@ohos.app.ability.UIAbility';
108
109class MyAbility extends UIAbility {
110    onContinue(wantParam: Record<string, Object>) {
111        return AbilityConstant.OnContinueResult.AGREE;
112    }
113}
114```
115
116## AbilityConstant.WindowMode
117
118Enumerates 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.
119
120**System capability**: SystemCapability.Ability.AbilityRuntime.Core
121
122**System API**: This is a system API and cannot be called by third-party applications.
123
124| Name                       | Value| Description                |
125| ---                         | --- | ---                  |
126| WINDOW_MODE_UNDEFINED       | 0   | Undefined window mode.      |
127| WINDOW_MODE_FULLSCREEN      | 1   | The ability is displayed in full screen.           |
128| WINDOW_MODE_SPLIT_PRIMARY   | 100 | The left screen in horizontal direction or the upper screen in vertical direction is the primary window.  |
129| WINDOW_MODE_SPLIT_SECONDARY | 101 | The right screen in horizontal direction or the lower screen in vertical direction is the secondary window.  |
130| WINDOW_MODE_FLOATING        | 102 | The ability is displayed in a floating window.|
131
132**Example**
133
134```ts
135import StartOptions from '@ohos.app.ability.StartOptions';
136import Want from '@ohos.app.ability.Want';
137import { BusinessError } from '@ohos.base';
138
139let want: Want = {
140    bundleName: 'com.example.myapplication',
141    abilityName: 'EntryAbility'
142};
143let option: StartOptions = {
144    windowMode: AbilityConstant.WindowMode.WINDOW_MODE_FULLSCREEN
145};
146
147// Ensure that the context is obtained.
148this.context.startAbility(want, option).then(()=>{
149    console.log('Succeed to start ability.');
150}).catch((error: BusinessError)=>{
151    console.error('Failed to start ability with error: ${JSON.stringify(error)}');
152});
153```
154
155## AbilityConstant.MemoryLevel
156
157Enumerates the memory levels. You can use it in [onMemoryLevel(level)](js-apis-app-ability-ability.md#abilityonmemorylevel) of [Ability](js-apis-app-ability-ability.md) to complete different operations.
158
159**System capability**: SystemCapability.Ability.AbilityRuntime.Core
160
161| Name                        | Value| Description               |
162| ---                         | --- | ---           |
163| MEMORY_LEVEL_MODERATE       | 0   | Moderate memory usage.|
164| MEMORY_LEVEL_LOW            | 1   | Low memory usage.  |
165| MEMORY_LEVEL_CRITICAL       | 2   | High memory usage.  |
166
167**Example**
168
169```ts
170import UIAbility from '@ohos.app.ability.UIAbility';
171
172class MyAbility extends UIAbility {
173    onMemoryLevel(level: AbilityConstant.MemoryLevel) {
174        if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
175            console.log('The memory of device is critical, please release some memory.');
176        }
177    }
178}
179```
180
181## AbilityConstant.OnSaveResult
182
183Enumerates 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 [Ability](js-apis-app-ability-uiAbility.md) to complete different operations.
184
185**System capability**: SystemCapability.Ability.AbilityRuntime.Core
186
187| Name                         | Value  | Description                                                        |
188| ----------------------------- | ---- | ------------------------------------------------------------ |
189| ALL_AGREE           | 0    | Always agreed to save the status.|
190| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
191| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
192| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
193| RECOVERY_REJECT  | 4    | Rejected to restore the saved state.|
194| ALL_REJECT  | 5    | Always rejected to save the status.|
195
196**Example**
197
198```ts
199import UIAbility from '@ohos.app.ability.UIAbility';
200
201class MyAbility extends UIAbility {
202    onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
203        return AbilityConstant.OnSaveResult.ALL_AGREE;
204    }
205}
206```
207
208## AbilityConstant.StateType
209
210Enumerates the scenarios for saving application data. You can use it in [onSaveState(reason, wantParam)](js-apis-app-ability-uiAbility.md#uiabilityonsavestate) of [Ability](js-apis-app-ability-uiAbility.md) to complete different operations.
211
212**System capability**: SystemCapability.Ability.AbilityRuntime.Core
213
214| Name                         | Value  | Description                                                        |
215| ----------------------------- | ---- | ------------------------------------------------------------ |
216| CONTINUATION           | 0    | Saving the status in continuation.|
217| APP_RECOVERY           | 1    | Saving the status in application recovery.|
218
219**Example**
220
221```ts
222import UIAbility from '@ohos.app.ability.UIAbility';
223
224class MyAbility extends UIAbility {
225    onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
226        if (reason === AbilityConstant.StateType.CONTINUATION) {
227            console.log('Save the ability data when the ability continuation.');
228        }
229        return AbilityConstant.OnSaveResult.ALL_AGREE;
230    }
231}
232```
233
234## AbilityConstant.ContinueState<sup>10+</sup>
235
236Enumerates 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).
237
238**System capability**: SystemCapability.Ability.AbilityRuntime.Core
239
240| Name          | Value      | Description                                                        |
241| ------------- | --------- | ------------------------------------------------------------ |
242| ACTIVE        | 0         | Mission continuation is activated for the current application.                             |
243| INACTIVE      | 1         | Mission continuation is not activated for the current application.                           |
244
245**Example**
246
247```ts
248  import { BusinessError } from '@ohos.base';
249
250  this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
251    console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
252  });
253```
254