• 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
43**Example**
44
45```ts
46import UIAbility from '@ohos.app.ability.UIAbility';
47
48class MyAbility extends UIAbility {
49    onCreate(want, launchParam) {
50        if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
51            console.log('The ability has been started by the way of startAbility.');
52        }
53    }
54}
55```
56
57## AbilityConstant.LastExitReason
58
59Enumerates 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.
60
61**System capability**: SystemCapability.Ability.AbilityRuntime.Core
62
63| Name                         | Value  | Description                                                        |
64| ----------------------------- | ---- | ------------------------------------------------------------ |
65| UNKNOWN          | 0    | Unknown reason.|
66| ABILITY_NOT_RESPONDING          | 1    | The ability does not respond.|
67| NORMAL | 2    | The ability exits normally.|
68
69**Example**
70
71```ts
72import UIAbility from '@ohos.app.ability.UIAbility';
73
74class MyAbility extends UIAbility {
75    onCreate(want, launchParam) {
76        if (launchParam.lastExitReason === AbilityConstant.LastExitReason.ABILITY_NOT_RESPONDING) {
77            console.log('The ability has exit last because the ability was not responding.');
78        }
79    }
80}
81```
82
83## AbilityConstant.OnContinueResult
84
85Enumerates 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.
86
87**System capability**: SystemCapability.Ability.AbilityRuntime.Core
88
89| Name                         | Value  | Description                                                        |
90| ----------------------------- | ---- | ------------------------------------------------------------ |
91| AGREE           | 0    | Continuation agreed.|
92| REJECT           | 1    | Continuation denied.|
93| MISMATCH  | 2    | Mismatch.|
94
95**Example**
96
97```ts
98import UIAbility from '@ohos.app.ability.UIAbility';
99
100class MyAbility extends UIAbility {
101    onContinue(wantParam) {
102        return AbilityConstant.OnContinueResult.AGREE;
103    }
104}
105```
106
107## AbilityConstant.WindowMode
108
109Enumerates 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.
110
111**System capability**: SystemCapability.Ability.AbilityRuntime.Core
112
113**System API**: This is a system API and cannot be called by third-party applications.
114
115| Name                       | Value| Description                |
116| ---                         | --- | ---                  |
117| WINDOW_MODE_UNDEFINED       | 0   | Undefined window mode.      |
118| WINDOW_MODE_FULLSCREEN      | 1   | The ability is displayed in full screen.           |
119| WINDOW_MODE_SPLIT_PRIMARY   | 100 | The left screen in horizontal direction or the upper screen in vertical direction is the primary window.  |
120| WINDOW_MODE_SPLIT_SECONDARY | 101 | The right screen in horizontal direction or the lower screen in vertical direction is the secondary window.  |
121| WINDOW_MODE_FLOATING        | 102 | The ability is displayed in a floating window.|
122
123**Example**
124
125```ts
126let want = {
127    bundleName: 'com.example.myapplication',
128    abilityName: 'EntryAbility'
129};
130let option = {
131    windowMode: AbilityConstant.WindowMode.WINDOW_MODE_FULLSCREEN
132};
133
134// Ensure that the context is obtained.
135this.context.startAbility(want, option).then(()=>{
136    console.log('Succeed to start ability.');
137}).catch((error)=>{
138    console.error('Failed to start ability with error: ${JSON.stringify(error)}');
139});
140```
141
142## AbilityConstant.MemoryLevel
143
144Enumerates 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.
145
146**System capability**: SystemCapability.Ability.AbilityRuntime.Core
147
148| Name                        | Value| Description               |
149| ---                         | --- | ---           |
150| MEMORY_LEVEL_MODERATE       | 0   | Moderate memory usage.|
151| MEMORY_LEVEL_LOW            | 1   | Low memory usage.  |
152| MEMORY_LEVEL_CRITICAL       | 2   | High memory usage.  |
153
154**Example**
155
156```ts
157import UIAbility from '@ohos.app.ability.UIAbility';
158
159class MyAbility extends UIAbility {
160    onMemoryLevel(level) {
161        if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
162            console.log('The memory of device is critical, please release some memory.');
163        }
164    }
165}
166```
167
168## AbilityConstant.OnSaveResult
169
170Enumerates 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.
171
172**System capability**: SystemCapability.Ability.AbilityRuntime.Core
173
174| Name                         | Value  | Description                                                        |
175| ----------------------------- | ---- | ------------------------------------------------------------ |
176| ALL_AGREE           | 0    | Always agreed to save the status.|
177| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
178| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
179| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
180| RECOVERY_REJECT  | 4    | Rejected to restore the saved state.|
181| ALL_REJECT  | 5    | Always rejected to save the status.|
182
183**Example**
184
185```ts
186import UIAbility from '@ohos.app.ability.UIAbility';
187
188class MyAbility extends UIAbility {
189    onSaveState(reason, wantParam) {
190        return AbilityConstant.OnSaveResult.ALL_AGREE;
191    }
192}
193```
194
195## AbilityConstant.StateType
196
197Enumerates 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.
198
199**System capability**: SystemCapability.Ability.AbilityRuntime.Core
200
201| Name                         | Value  | Description                                                        |
202| ----------------------------- | ---- | ------------------------------------------------------------ |
203| CONTINUATION           | 0    | Saving the status in continuation.|
204| APP_RECOVERY           | 1    | Saving the status in application recovery.|
205
206**Example**
207
208```ts
209import UIAbility from '@ohos.app.ability.UIAbility';
210
211class MyAbility extends UIAbility {
212    onSaveState(reason, wantParam) {
213        if (reason === AbilityConstant.StateType.CONTINUATION) {
214            console.log('Save the ability data when the ability continuation.');
215        }
216        return AbilityConstant.OnSaveResult.ALL_AGREE;
217    }
218}
219```
220