• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UIAbility Usage
2
3
4When using the UIAbility component, you must specify a startup page and obtain the context, [UIAbilityContext](../reference/apis/js-apis-inner-application-uiAbilityContext.md).
5
6
7## Specifying the Startup Page of UIAbility
8
9You can use **loadContent()** of [WindowStage](../reference/apis/js-apis-window.md#windowstage9) to set the startup page in the **onWindowStageCreate()** callback of the UIAbility instance. If no startup page is specified, a white screen occurs after the application is started.
10
11
12```ts
13import UIAbility from '@ohos.app.ability.UIAbility';
14import window from '@ohos.window';
15
16export default class EntryAbility extends UIAbility {
17  onWindowStageCreate(windowStage: window.WindowStage) {
18    // Main window is created. Set a main page for this ability.
19    windowStage.loadContent('pages/Index', (err, data) => {
20      // ...
21    });
22  }
23
24  // ...
25}
26```
27
28> **NOTE**
29>
30> When you create UIAbility in DevEco Studio, the UIAbility instance loads the **Index** page as its startup page. Therefore, you only need to replace the **Index** page path with the required startup page path.
31
32
33## Obtaining the Context of UIAbility
34
35The UIAbility class has its own context, which is an instance of the [UIAbilityContext](../reference/apis/js-apis-inner-application-uiAbilityContext.md) class. The UIAbilityContext class has attributes such as **abilityInfo** and **currentHapModuleInfo**. UIAbilityContext can be used to obtain the UIAbility configuration information, such as the bundle code path, bundle name, ability name, and environment status required by the application. It can also be used to obtain methods to operate the UIAbility instance, such as **startAbility()**, **connectServiceExtensionAbility()**, and **terminateSelf()**.
36
37- You can use **this.context** to obtain the context of a UIAbility instance.
38
39  ```ts
40  import UIAbility from '@ohos.app.ability.UIAbility';
41  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
42  import Want from '@ohos.app.ability.Want';
43
44  export default class EntryAbility extends UIAbility {
45    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
46      // Obtain the context of the UIAbility instance.
47      let context = this.context;
48      ...
49    }
50  }
51  ```
52
53- Import the context module and define the **context** variable in the component.
54
55  ```ts
56  import common from '@ohos.app.ability.common';
57  import Want from '@ohos.app.ability.Want';
58
59  @Entry
60  @Component
61  struct Index {
62    private context = getContext(this) as common.UIAbilityContext;
63
64    startAbilityTest() {
65      let want: Want = {
66        // Want parameter information.
67      };
68      this.context.startAbility(want);
69    }
70
71    // Page display.
72    build() {
73      ...
74    }
75  }
76  ```
77
78  You can also define variables after importing the context module but before using [UIAbilityContext](../reference/apis/js-apis-inner-application-uiAbilityContext.md).
79
80
81  ```ts
82  import common from '@ohos.app.ability.common';
83  import Want from '@ohos.app.ability.Want';
84
85  @Entry
86  @Component
87  struct Index {
88
89    startAbilityTest() {
90      let context = getContext(this) as common.UIAbilityContext;
91      let want: Want = {
92        // Want parameter information.
93      };
94      context.startAbility(want);
95    }
96
97    // Page display.
98    build() {
99      ...
100    }
101  }
102  ```
103