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. 7> - This API applies only to the stage model. 8 9getContext(component?: Object):Context 10 11Obtains the **Context** object associated with an ability on the page. 12 13**Atomic service API**: This API can be used in atomic services since API version 11. 14 15**System capability**: SystemCapability.ArkUI.ArkUI.Full 16 17**Parameters** 18 19| Name| Type | Mandatory| Description | 20| ------ | ----------- | ---- | ------------------------------- | 21| 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. | 22 23**Return value** 24 25| Type| Description | 26| ------ | ------------------------------- | 27| [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. | 28 29**Example** 30 31Load a page by calling **windowStage.loadContent** in the UIAbility. 32 33> **NOTE** 34> 35> To avoid confusion with instances, it is recommended that you obtain a **UIContext** instance using the [getUIContext](js-apis-arkui-UIContext.md#uicontext) API, and then call **getContext** bound to the context through the [getHostContext](js-apis-arkui-UIContext.md#gethostcontext12) API. 36 37```ts 38// EntryAbility.ets 39import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 40import { hilog } from '@kit.PerformanceAnalysisKit'; 41import { window } from '@kit.ArkUI'; 42 43export default class EntryAbility extends UIAbility { 44 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 45 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 46 } 47 48 onDestroy() { 49 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 50 } 51 52 onWindowStageCreate(windowStage: window.WindowStage) { 53 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 54 55 windowStage.loadContent('pages/Index', (err, data) => { 56 if (err.code) { 57 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 58 return; 59 } 60 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 61 }); 62 } 63 64 onWindowStageDestroy() { 65 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 66 } 67 68 onForeground() { 69 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 70 } 71 72 onBackground() { 73 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 74 } 75} 76``` 77In **Index.ets**, **getContext** is used to obtain the context. In this example, the return value type is UIAbilityContext. 78 79```ts 80//pages/Index.ets 81@Entry 82@Component 83struct Index { 84 @State message: string = 'Hello World' 85 86 build() { 87 Row() { 88 Column() { 89 Text(this.message) 90 .fontSize(50) 91 .fontWeight(FontWeight.Bold) 92 .onClick(() => { 93 // You are advised to use this.getUIContext().getHostContext(). 94 let context : Context = getContext(this) as Context 95 console.info("CacheDir:" + context.cacheDir) 96 }) 97 } 98 .width('100%') 99 } 100 .height('100%') 101 } 102} 103``` 104