• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.Ability (Ability Base Class)
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: Ability-->
5<!--Owner: @littlejerry1-->
6<!--Designer: @ccllee1-->
7<!--Tester: @lixueqing513-->
8<!--Adviser: @huipeizi-->
9
10The Ability class is the fundamental unit for application lifecycle scheduling. It is the base class of [UIAbility](js-apis-app-ability-uiAbility.md) and [ExtensionAbility](js-apis-app-ability-extensionAbility.md), and provides callbacks for system configuration updates and memory level updates. However, you cannot inherit directly from this base class. You should opt for either [UIAbility](js-apis-app-ability-uiAbility.md) or [ExtensionAbility](js-apis-app-ability-extensionAbility.md) based on your service needs. For details, see [Introduction to Ability Kit](../../application-models/abilitykit-overview.md).
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 { Ability } from '@kit.AbilityKit';
22```
23
24## Ability Inheritance Relationship
25
26The following figure shows the inheritance relationship of the Ability base class and its child classes.
27
28> **NOTE**
29>
30> Some ExtensionAbility components (such as [FormExtensionAbility](../apis-form-kit/js-apis-app-form-formExtensionAbility.md) and [InputMethodExtensionAbility](../apis-ime-kit/js-apis-inputmethod-extension-ability.md)) do not inherit from the ExtensionAbility base class and therefore are not provided in the following figure.
31
32![ability-inheritance](../figures/image-ability-ability-inheritance.png)
33
34## Ability.onConfigurationUpdate
35
36onConfigurationUpdate(newConfig: Configuration): void
37
38Called when a system environment variable changes. You can override this callback to respond to changes in the system environment variables. For example, when the system language changes, the application can perform customized processing in the callback.
39
40> **NOTE**
41>
42> There are certain restrictions when this callback is actually triggered. If you set the application language by calling [setLanguage](../apis-ability-kit/js-apis-inner-application-applicationContext.md#applicationcontextsetlanguage11), the system does not trigger the **onConfigurationUpdate** callback even if the system language changes. For details, see [When to Use](../../application-models/subscribe-system-environment-variable-changes.md#when-to-use).
43
44**Atomic service API**: This API can be used in atomic services since API version 11.
45
46**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
47
48**Parameters**
49
50| Name| Type| Mandatory| Description|
51| -------- | -------- | -------- | -------- |
52| newConfig | [Configuration](js-apis-app-ability-configuration.md) | Yes| New configuration.|
53
54**Example**
55
56```ts
57// You are not allowed to inherit from the top-level base class Ability. Therefore, the derived class UIAbility is used as an example.
58import { UIAbility, Configuration } from '@kit.AbilityKit';
59
60class MyUIAbility extends UIAbility {
61  onConfigurationUpdate(config: Configuration) {
62    console.log(`onConfigurationUpdate, config: ${JSON.stringify(config)}`);
63  }
64}
65```
66
67## Ability.onMemoryLevel
68
69onMemoryLevel(level: AbilityConstant.MemoryLevel): void
70
71Called when the available memory of the entire device changes to a specified level. You can override this callback to respond to changes in the memory level, for example, releasing cached data.
72
73**Atomic service API**: This API can be used in atomic services since API version 11.
74
75**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
76
77**Parameters**
78
79| Name| Type| Mandatory| Description|
80| -------- | -------- | -------- | -------- |
81| level | [AbilityConstant.MemoryLevel](js-apis-app-ability-abilityConstant.md#memorylevel) | Yes| Level of the available memory.<br>**NOTE**<br>The trigger conditions may differ across various devices. For example, on a standard device with 12 GB of memory:<br>- When the available memory of the entire device drops to 1700 MB to 1800 MB, the **onMemoryLevel** callback of the MEMORY_LEVEL_MODERATE type is triggered, indicating that the available memory is moderate.<br>- When the available memory of the entire device drops to 1600 MB to 1700 MB, the **onMemoryLevel** callback of the MEMORY_LEVEL_LOW type is triggered, indicating that the available memory is low.<br>- When the available memory of the entire device drops below 1600 MB, the **onMemoryLevel** callback of the MEMORY_LEVEL_CRITICAL type is triggered, indicating that the available memory is critically low.|
82
83**Example**
84
85```ts
86// You are not allowed to inherit from the top-level base class Ability. Therefore, the derived class UIAbility is used as an example.
87import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
88
89class MyUIAbility extends UIAbility {
90  onMemoryLevel(level: AbilityConstant.MemoryLevel) {
91    console.log(`onMemoryLevel, level: ${JSON.stringify(level)}`);
92  }
93}
94```
95