js-apis-app-ability-abilityConstant.md 9.0 KB
Newer Older
1
# @ohos.app.ability.AbilityConstant (AbilityConstant)
2

3
The **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 6 7 8 9 10 11 12 13 14 15 16 17

> **NOTE**
> 
> 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. 
> The APIs of this module can be used only in the stage model.

## Modules to Import

```ts
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
```

## Attributes

18 19 20 21
## AbilityConstant.LaunchParam

Defines the parameters for starting an ability.

22 23
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

24
| Name| Type| Readable| Writable| Description|
25
| -------- | -------- | -------- | -------- | -------- |
26 27
| launchReason | [LaunchReason](#abilityconstantlaunchreason)| Yes| Yes| Ability launch reason, which is an enumerated type.|
| lastExitReason | [LastExitReason](#abilityconstantlastexitreason) | Yes| Yes| Reason for the last exit, which is an enumerated type.|
28 29 30

## AbilityConstant.LaunchReason

31
Enumerates 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 34 35 36 37

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| UNKNOWN          | 0    | Unknown reason.|
G
Gloria 已提交
38 39
| START_ABILITY          | 1    | The ability is started by calling [startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability).|
| CALL | 2    | The ability is started by calling [startAbilityByCall](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilitybycall).|
40 41 42 43
| CONTINUATION           | 3    | The ability is started by means of cross-device migration.|
| APP_RECOVERY           | 4    | The ability is automatically started when the application is restored from a fault.|

**Example**
44

45 46 47 48 49 50
```ts
import UIAbility from '@ohos.app.ability.UIAbility';

class MyAbility extends UIAbility {
    onCreate(want, launchParam) {
        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 58

## AbilityConstant.LastExitReason

59
Enumerates 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 62 63 64 65 66

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| UNKNOWN          | 0    | Unknown reason.|
| ABILITY_NOT_RESPONDING          | 1    | The ability does not respond.|
67
| NORMAL | 2    | The ability exits normally.|
68

69 70 71 72 73 74 75 76
**Example**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';

class MyAbility extends UIAbility {
    onCreate(want, launchParam) {
        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 84

## AbilityConstant.OnContinueResult 

85
Enumerates 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 88 89 90 91 92 93 94

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| AGREE           | 0    | Continuation agreed.|
| REJECT           | 1    | Continuation denied.|
| MISMATCH  | 2    | Mismatch.|

95 96 97 98 99 100 101
**Example**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';

class MyAbility extends UIAbility {
    onContinue(wantParam) {
G
Gloria 已提交
102
        return AbilityConstant.OnContinueResult.AGREE;
103 104 105 106
    }
}
```

107 108
## AbilityConstant.WindowMode

109
Enumerates 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 112

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

113 114
**System API**: This is a system API and cannot be called by third-party applications.

115 116 117 118
| Name                       | Value| Description                |
| ---                         | --- | ---                  |
| WINDOW_MODE_UNDEFINED       | 0   | Undefined window mode.      |
| WINDOW_MODE_FULLSCREEN      | 1   | The ability is displayed in full screen.           |
119 120
| WINDOW_MODE_SPLIT_PRIMARY   | 100 | The left screen in horizontal direction or the upper screen in vertical direction is the primary window.  |
| WINDOW_MODE_SPLIT_SECONDARY | 101 | The right screen in horizontal direction or the lower screen in vertical direction is the secondary window.  |
121 122
| WINDOW_MODE_FLOATING        | 102 | The ability is displayed in a floating window.|

123 124 125 126
**Example**

```ts
let want = {
127 128
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
129 130 131 132 133 134
};
let option = {
    windowMode: AbilityConstant.WindowMode.WINDOW_MODE_FULLSCREEN
};

// Ensure that the context is obtained.
G
Gloria 已提交
135
this.context.startAbility(want, option).then(()=>{
136
    console.log('Succeed to start ability.');
137
}).catch((error)=>{
G
Gloria 已提交
138
    console.error('Failed to start ability with error: ${JSON.stringify(error)}');
139 140 141
});
```

142 143
## AbilityConstant.MemoryLevel

144
Enumerates 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 147 148

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                        | Value| Description               |
149 150 151
| ---                         | --- | ---           |
| MEMORY_LEVEL_MODERATE       | 0   | Moderate memory usage.|
| MEMORY_LEVEL_LOW            | 1   | Low memory usage.  |
152 153
| MEMORY_LEVEL_CRITICAL       | 2   | High memory usage.  |

154 155 156 157 158 159 160 161
**Example**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';

class MyAbility extends UIAbility {
    onMemoryLevel(level) {
        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 169
## AbilityConstant.OnSaveResult

170
Enumerates 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 173 174 175

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
176
| ALL_AGREE           | 0    | Always agreed to save the status.|
177 178 179 180
| CONTINUATION_REJECT           | 1    | Rejected to save the status in continuation.|
| CONTINUATION_MISMATCH  | 2    | Continuation mismatch.|
| RECOVERY_AGREE           | 3    | Agreed to restore the saved status.|
| RECOVERY_REJECT  | 4    | Rejected to restore the saved state.|
181 182 183 184 185 186 187 188 189 190 191 192 193
| ALL_REJECT  | 5    | Always rejected to save the status.|

**Example**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';

class MyAbility extends UIAbility {
    onSaveState(reason, wantParam) {
        return AbilityConstant.OnSaveResult.ALL_AGREE;
    }
}
```
194 195 196

## AbilityConstant.StateType

197
Enumerates 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 200 201 202 203 204

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| CONTINUATION           | 0    | Saving the status in continuation.|
| APP_RECOVERY           | 1    | Saving the status in application recovery.|
205 206 207 208 209 210 211 212 213

**Example**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';

class MyAbility extends UIAbility {
    onSaveState(reason, wantParam) {
        if (reason === AbilityConstant.StateType.CONTINUATION) {
214
            console.log('Save the ability data when the ability continuation.');
215 216 217 218 219
        } 
        return AbilityConstant.OnSaveResult.ALL_AGREE;
    }
}
```