We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
ReferencesApplication FrameworkArkUIArkTS APIUIgetContext

getContext

To obtain the current Ability's Context within a page, call the getContext API to obtain the UIAbilityContext or ExtensionContext associated with the current page.

NOTE
  • 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.
  • This API applies only to the stage model.

getContext(deprecated)

PhonePC/2in1TabletTVWearable

getContext(component?: Object):Context

Obtains the Context object associated with an ability on the page.

NOTE

This API is supported since API version 9 and deprecated since API version 18. You are advised to use getHostContext instead. getHostContext has effect only after first a UIContext instance is obtained.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

Model restriction: This API can be used only in the stage model.

Parameters

Expand
Name Type Mandatory Description
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.

Return value

Expand
Type Description
Context 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.

Context

PhonePC/2in1TabletTVWearable

type Context = Context

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

Model restriction: This API can be used only in the stage model.

Expand
Type Description
Context 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.
NOTE

Directly using getContext can lead to the issue of ambiguous UI context. To avoid this, obtain a UIContext instance using getUIContext(), and then obtain the associated Context object using getHostContext.

Example

Load a page by calling windowStage.loadContent in the UIAbility.

Collapse
Word wrap
Dark theme
Copy code
  1. // EntryAbility.ets
  2. import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
  3. import { hilog } from '@kit.PerformanceAnalysisKit';
  4. import { window } from '@kit.ArkUI';
  5. export default class EntryAbility extends UIAbility {
  6. onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
  7. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  8. }
  9. onDestroy() {
  10. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  11. }
  12. onWindowStageCreate(windowStage: window.WindowStage) {
  13. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  14. windowStage.loadContent('pages/Index', (err, data) => {
  15. if (err.code) {
  16. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  17. return;
  18. }
  19. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  20. });
  21. }
  22. onWindowStageDestroy() {
  23. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  24. }
  25. onForeground() {
  26. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  27. }
  28. onBackground() {
  29. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  30. }
  31. }

In Index.ets, getContext is used to obtain the context. In this example, the return value type is UIAbilityContext.

Collapse
Word wrap
Dark theme
Copy code
  1. //pages/Index.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State message: string = 'Hello World'
  6. build() {
  7. Row() {
  8. Column() {
  9. Text(this.message)
  10. .fontSize(50)
  11. .fontWeight(FontWeight.Bold)
  12. .onClick(() => {
  13. // You are advised to use this.getUIContext().getHostContext().
  14. let context: Context = getContext(this) as Context;
  15. console.info("CacheDir:" + context.cacheDir);
  16. })
  17. }
  18. .width('100%')
  19. }
  20. .height('100%')
  21. }
  22. }
Search in References
Enter a keyword.