• 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 | Ability ID. If the ability with this ID 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## AbilityStage.onNewProcessRequest<sup>11+</sup>
74
75onNewProcessRequest(want: Want): string
76
77Called when the UIAbility is started in the specified process.
78
79**System capability**: SystemCapability.Ability.AbilityRuntime.Core
80
81**Parameters**
82
83| Name| Type| Mandatory| Description|
84| -------- | -------- | -------- | -------- |
85| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.|
86
87**Return value**
88
89| Type| Description|
90| -------- | -------- |
91| string | Process ID. If the process with this ID has been created, the ability runs in the process. Otherwise, a new process is created and the ability runs in it.|
92
93**Example**
94
95```ts
96import AbilityStage from '@ohos.app.ability.AbilityStage';
97import Want from '@ohos.app.ability.Want';
98
99class MyAbilityStage extends AbilityStage {
100    onNewProcessRequest(want: Want) {
101        console.log('MyAbilityStage.onNewProcessRequest called');
102        return 'com.example.test';
103    }
104}
105```
106
107
108## AbilityStage.onConfigurationUpdate
109
110onConfigurationUpdate(newConfig: Configuration): void
111
112Called when the global configuration is updated.
113
114**System capability**: SystemCapability.Ability.AbilityRuntime.Core
115
116**Parameters**
117
118  | Name| Type| Mandatory| Description|
119  | -------- | -------- | -------- | -------- |
120  | 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.|
121
122**Example**
123
124```ts
125import AbilityStage from '@ohos.app.ability.AbilityStage';
126import { Configuration } from '@ohos.app.ability.Configuration';
127
128class MyAbilityStage extends AbilityStage {
129    onConfigurationUpdate(config: Configuration) {
130        console.log('onConfigurationUpdate, language: ${config.language}');
131    }
132}
133```
134
135## AbilityStage.onMemoryLevel
136
137onMemoryLevel(level: AbilityConstant.MemoryLevel): void
138
139Called 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.
140
141**System capability**: SystemCapability.Ability.AbilityRuntime.Core
142
143**Parameters**
144
145  | Name| Type| Mandatory| Description|
146  | -------- | -------- | -------- | -------- |
147  | 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.|
148
149**Example**
150
151```ts
152import AbilityStage from '@ohos.app.ability.AbilityStage';
153import AbilityConstant from '@ohos.app.ability.AbilityConstant';
154
155class MyAbilityStage extends AbilityStage {
156    onMemoryLevel(level: AbilityConstant.MemoryLevel) {
157        console.log(`onMemoryLevel, level: ${JSON.stringify(level)}`);
158    }
159}
160```
161
162## AbilityStage.context
163
164context: AbilityStageContext
165
166Defines the context of **AbilityStage**.
167
168**System capability**: SystemCapability.Ability.AbilityRuntime.Core
169
170| Name     | Type                       | Description                                                        |
171| ----------- | --------------------------- | ------------------------------------------------------------ |
172| context  | [AbilityStageContext](js-apis-inner-application-abilityStageContext.md) | The context is obtained in the callback invoked when initialization is performed during ability startup.|
173
174**Example**
175
176```ts
177import AbilityStage from '@ohos.app.ability.AbilityStage';
178
179export default class MyAbilityStage extends AbilityStage {
180  onCreate() {
181    let abilityStageContext = this.context;
182  }
183  // ...
184}
185```
186