• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# getContext
2
3The **getContext** API enables you to obtain the context of the ability (either UIAbilityContext or ExtensionContext) on the current page.
4
5> **NOTE**
6> - This API is supported since API version 9 and deprecated since API version 18. You are advised to use [getHostContext](js-apis-arkui-UIContext.md#gethostcontext12) in [UIContext](js-apis-arkui-UIContext.md#uicontext) instead.
7> - This API applies only to the stage model.
8
9## getContext<sup>(deprecated)</sup>
10
11getContext(component?: Object):Context
12
13Obtains the **Context** object associated with an ability on the page.
14
15> **NOTE**
16>
17> This API deprecated since API version 18. You are advised to use [getHostContext](js-apis-arkui-UIContext.md#gethostcontext12) in [UIContext](js-apis-arkui-UIContext.md#uicontext) instead.
18>
19> Since API version 12, you can use the [getHostContext](js-apis-arkui-UIContext.md#gethostcontext12) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
20
21**Atomic service API**: This API can be used in atomic services since API version 11.
22
23**System capability**: SystemCapability.ArkUI.ArkUI.Full
24
25**Parameters**
26
27| Name| Type         | Mandatory| Description                            |
28| ------ | ----------- | ---- | ------------------------------- |
29| component  | Object | No  | Ability instance. If no component is passed in or the passed-in parameter type is invalid, the default context is returned. The default context is the context obtained by tracing the call chain of the API. If this API is used in an asynchronous callback or not initially called on the current page, the context of the instance may fail to be traced. In this case, **undefined** is returned.            |
30
31**Return value**
32
33| Type| Description                            |
34| ------ | ------------------------------- |
35| [Context](../../application-models/application-context-stage.md) | Context of the ability. The context type depends on the ability type. For example, if this API is called on a page of the UIAbility, the return value type is UIAbilityContext; if this API is called on a page of the ExtensionAbility, the return value type is ExtensionContext.   |
36
37**Example**
38
39Load a page by calling **windowStage.loadContent** in the UIAbility.
40
41> **NOTE**
42>
43> Directly using **getContext** can lead to the issue of [ambiguous UI context](../../ui/arkts-global-interface.md#ambiguous-ui-context). To avoid this, obtain a **UIContext** instance using [getUIContext](js-apis-arkui-UIContext.md#uicontext), and then obtain the associated **Context** object using [getHostContext](js-apis-arkui-UIContext.md#gethostcontext12).
44
45```ts
46// EntryAbility.ets
47import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
48import { hilog } from '@kit.PerformanceAnalysisKit';
49import { window } from '@kit.ArkUI';
50
51export default class EntryAbility extends UIAbility {
52  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
53    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
54  }
55
56  onDestroy() {
57    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
58  }
59
60  onWindowStageCreate(windowStage: window.WindowStage) {
61    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
62
63    windowStage.loadContent('pages/Index', (err, data) => {
64      if (err.code) {
65        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
66        return;
67      }
68      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
69    });
70  }
71
72  onWindowStageDestroy() {
73    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
74  }
75
76  onForeground() {
77    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
78  }
79
80  onBackground() {
81    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
82  }
83}
84```
85In **Index.ets**, **getContext** is used to obtain the context. In this example, the return value type is UIAbilityContext.
86
87```ts
88//pages/Index.ets
89@Entry
90@Component
91struct Index {
92  @State message: string = 'Hello World'
93
94  build() {
95    Row() {
96      Column() {
97        Text(this.message)
98          .fontSize(50)
99          .fontWeight(FontWeight.Bold)
100          .onClick(() => {
101            // You are advised to use this.getUIContext().getHostContext().
102            let context: Context = getContext(this) as Context;
103            console.info("CacheDir:" + context.cacheDir);
104          })
105      }
106      .width('100%')
107    }
108    .height('100%')
109  }
110}
111```
112