• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.AbilityStage (AbilityStage)
2
3**AbilityStage** is a runtime class for HAP files.
4
5**AbilityStage** notifies you of when you can perform HAP initialization such as resource pre-loading and thread creation during the HAP loading.
6
7> **NOTE**
8>
9> 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.
10> The APIs of this module can be used only in the stage model.
11
12## Modules to Import
13
14```ts
15import AbilityStage from '@ohos.app.ability.AbilityStage';
16```
17
18## AbilityStage.onCreate
19
20onCreate(): void
21
22Called when the application is created.
23
24**System capability**: SystemCapability.Ability.AbilityRuntime.Core
25
26**Example**
27
28```ts
29import AbilityStage from '@ohos.app.ability.AbilityStage';
30
31class MyAbilityStage extends AbilityStage {
32    onCreate() {
33        console.log('MyAbilityStage.onCreate is called');
34    }
35}
36```
37
38
39## AbilityStage.onAcceptWant
40
41onAcceptWant(want: Want): string;
42
43Called when a specified ability is started.
44
45**System capability**: SystemCapability.Ability.AbilityRuntime.Core
46
47**Parameters**
48
49| Name| Type| Mandatory| Description|
50| -------- | -------- | -------- | -------- |
51| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.|
52
53**Return value**
54
55  | Type| Description|
56  | -------- | -------- |
57  | string | Returns an ability ID. If this ability has been started, no new instance is created and the ability is placed at the top of the stack. Otherwise, a new instance is created and started.|
58
59**Example**
60
61```ts
62import AbilityStage from '@ohos.app.ability.AbilityStage';
63import Want from '@ohos.app.ability.Want';
64
65class MyAbilityStage extends AbilityStage {
66    onAcceptWant(want: Want) {
67        console.log('MyAbilityStage.onAcceptWant called');
68        return 'com.example.test';
69    }
70}
71```
72
73
74## AbilityStage.onConfigurationUpdate
75
76onConfigurationUpdate(newConfig: Configuration): void;
77
78Called when the global configuration is updated.
79
80**System capability**: SystemCapability.Ability.AbilityRuntime.Core
81
82**Parameters**
83
84  | Name| Type| Mandatory| Description|
85  | -------- | -------- | -------- | -------- |
86  | newConfig | [Configuration](js-apis-app-ability-configuration.md) | Yes| Callback invoked when the global configuration is updated. The global configuration indicates the configuration of the environment where the application is running and includes the language and color mode.|
87
88**Example**
89
90```ts
91import AbilityStage from '@ohos.app.ability.AbilityStage';
92import { Configuration } from '@ohos.app.ability.Configuration';
93
94class MyAbilityStage extends AbilityStage {
95    onConfigurationUpdate(config: Configuration) {
96        console.log('onConfigurationUpdate, language: ${config.language}');
97    }
98}
99```
100
101## AbilityStage.onMemoryLevel
102
103onMemoryLevel(level: AbilityConstant.MemoryLevel): void;
104
105Called when the system has decided to adjust the memory level. For example, this API can be used when there is not enough memory to run as many background processes as possible.
106
107**System capability**: SystemCapability.Ability.AbilityRuntime.Core
108
109**Parameters**
110
111  | Name| Type| Mandatory| Description|
112  | -------- | -------- | -------- | -------- |
113  | level | [AbilityConstant.MemoryLevel](js-apis-app-ability-abilityConstant.md#abilityconstantmemorylevel) | Yes| Memory level that indicates the memory usage status. When the specified memory level is reached, a callback will be invoked and the system will start adjustment.|
114
115**Example**
116
117```ts
118import AbilityStage from '@ohos.app.ability.AbilityStage';
119import AbilityConstant from '@ohos.app.ability.AbilityConstant';
120
121class MyAbilityStage extends AbilityStage {
122    onMemoryLevel(level: AbilityConstant.MemoryLevel) {
123        console.log(`onMemoryLevel, level: ${JSON.stringify(level)}`);
124    }
125}
126```
127
128## AbilityStage.context
129
130context: AbilityStageContext;
131
132Defines the context of **AbilityStage**.
133
134**System capability**: SystemCapability.Ability.AbilityRuntime.Core
135
136| Name     | Type                       | Description                                                        |
137| ----------- | --------------------------- | ------------------------------------------------------------ |
138| context  | [AbilityStageContext](js-apis-inner-application-abilityStageContext.md) | The context is obtained in the callback invoked when initialization is performed during ability startup.|
139