The current repo belongs to Closed status, and some functions are restricted. For details, please refer to the description of repo status
Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
docker
en
application-dev
IDL
accessibility
ads-service
ai
application-models
application-test
arkts-utils
basic-services
calendarmanager
connectivity
contacts
database
device-usage-statistics
device
dfx
displaymanager
distributedservice
enterprise-device-management
faqs
ffrt
file-management
form
graphics
graphics3d
inputmethod
internationalization
ipc
mdm
media
napi
network
notification
performance
public_sys-resources
quick-start
reference
apis-ability-kit
apis-accessibility-kit
apis-ads-kit
apis-arkdata
apis-arkgraphics2d
apis-arkgraphics3d
apis-arkts
apis-arkui
arkui-js-lite
arkui-js
arkui-ts
figures
js-service-widget-ui
Readme-EN.md
_a_r_k_u_i___text_picker_cascade_range_content.md
_a_r_k_u_i___text_picker_range_content.md
_ark_u_i___animate_complete_callback.md
_ark_u_i___attribute_item.md
_ark_u_i___color_stop.md
_ark_u_i___context_callback.md
_ark_u_i___event_module.md
_ark_u_i___expected_frame_rate_range.md
_ark_u_i___int_offset.md
_ark_u_i___int_size.md
_ark_u_i___margin.md
_ark_u_i___native_animate_a_p_i__1.md
_ark_u_i___native_dialog_a_p_i__1.md
_ark_u_i___native_gesture_a_p_i__1.md
_ark_u_i___native_module.md
_ark_u_i___native_node_a_p_i__1.md
_ark_u_i___node_component_event.md
_ark_u_i___rect.md
_ark_u_i___rotation_options.md
_ark_u_i___scale_options.md
_ark_u_i___string_async_event.md
_ark_u_i___translation_options.md
_native_display_manager___cutout_info.md
_native_display_manager___rect.md
_o_h___display_manager.md
_o_h___native_x_component.md
_o_h___native_x_component___callback.md
_o_h___native_x_component___expected_rate_range.md
_o_h___native_x_component___mouse_event.md
_o_h___native_x_component___mouse_event___callback.md
_o_h___native_x_component___touch_event.md
_o_h___native_x_component___touch_point.md
_window_manager___native_module.md
drag__and__drop_8h.md
drawable__descriptor_8h.md
errorcode-animator.md
errorcode-bindSheet.md
errorcode-display.md
errorcode-drag-event.md
errorcode-focus.md
errorcode-image-analyzer.md
errorcode-promptAction.md
errorcode-router.md
errorcode-system-resource.md
errorcode-uiappearance.md
errorcode-window.md
ive_display_manager___waterfall_display_area_rects.md
js-apis-StateManagement.md
js-apis-animator.md
js-apis-application-windowExtensionAbility-sys.md
js-apis-arkui-AttributeUpdater.md
js-apis-arkui-ComponentContent.md
js-apis-arkui-Content.md
js-apis-arkui-NodeContent.md
js-apis-arkui-Prefetcher.md
js-apis-arkui-UIContext-sys.md
js-apis-arkui-UIContext.md
js-apis-arkui-builderNode.md
js-apis-arkui-componentSnapshot.md
js-apis-arkui-componentUtils.md
js-apis-arkui-dragController.md
js-apis-arkui-drawableDescriptor-sys.md
js-apis-arkui-drawableDescriptor.md
js-apis-arkui-frameNode.md
js-apis-arkui-graphics.md
js-apis-arkui-inspector.md
js-apis-arkui-node.md
js-apis-arkui-nodeController.md
js-apis-arkui-observer.md
js-apis-arkui-performancemonitor-sys.md
js-apis-arkui-renderNode.md
js-apis-arkui-shape.md
js-apis-arkui-theme.md
js-apis-arkui-uiExtension-sys.md
js-apis-arkui-uiExtension.md
js-apis-arkui-xcomponentNode.md
js-apis-curve.md
js-apis-devicestatus-draginteraction-sys.md
js-apis-display-sys.md
js-apis-display.md
js-apis-font.md
js-apis-getContext.md
js-apis-inner-application-windowExtensionContext-sys.md
js-apis-matrix4.md
js-apis-measure.md
js-apis-mediaquery.md
js-apis-pipWindow.md
js-apis-plugincomponent-sys.md
js-apis-plugincomponent.md
js-apis-postCardAction.md
js-apis-prompt.md
js-apis-promptAction-sys.md
js-apis-promptAction.md
js-apis-router.md
js-apis-screen-sys.md
js-apis-screenshot-sys.md
js-apis-screenshot.md
js-apis-system-app.md
js-apis-system-configuration.md
js-apis-system-mediaquery.md
js-apis-system-prompt.md
js-apis-system-router.md
js-apis-uiExtensionHost-sys.md
js-apis-uiappearance-sys.md
js-apis-window-sys.md
js-apis-window.md
js-apis-windowAnimationManager-sys.md
native__animate_8h.md
native__dialog_8h.md
native__gesture_8h.md
native__interface_8h.md
native__interface__xcomponent_8h.md
native__node_8h.md
native__node__napi_8h.md
native__type_8h.md
native__xcomponent__key__event_8h.md
oh__display__info_8h.md
oh__display__manager_8h.md
oh__window__comm_8h.md
oh__window__event__filter_8h.md
styled__string_8h.md
ui__input__event_8h.md
union_ark_u_i___number_value.md
apis-arkweb
apis-asset-store-kit
apis-audio-kit
apis-avcodec-kit
apis-avsession-kit
apis-backgroundtasks-kit
apis-basic-services-kit
apis-calendar-kit
apis-camera-kit
apis-connectivity-kit
apis-contacts-kit
apis-core-file-kit
apis-crypto-architecture-kit
apis-data-protection-kit
apis-device-certificate-kit
apis-distributedservice-kit
apis-driverdevelopment-kit
apis-drm-kit
apis-ffrt-kit
apis-form-kit
apis-image-kit
apis-ime-kit
apis-input-kit
apis-ipc-kit
apis-localization-kit
apis-location-kit
apis-mdm-kit
apis-media-kit
apis-media-library-kit
apis-mindspore-lite-kit
apis-multimodalawareness-kit
apis-network-kit
apis-neural-network-runtime-kit
apis-notification-kit
apis-performance-analysis-kit
apis-sensor-service-kit
apis-telephony-kit
apis-test-kit
apis-universal-keystore-kit
apis-user-authentication-kit
common
figures
native-lib
Readme-EN.md
development-intro.md
errorcode-universal.md
syscap-list.md
syscap.md
security
task-management
telephony
tools
ui
web
webgl
windowmanager
Readme-EN.md
application-dev-guide-for-gitee.md
application-dev-guide.md
website.md
contribute
design
device-dev
figures
readme
release-notes
third-party-components
Legal-Notices.md
OpenHarmony-Overview.md
glossary.md
readme.md
website.md
zh-cn
.gitattributes
.gitignore
CODEOWNERS
DCO.txt
LICENSE
OAT.xml
README.md
README_zh.md
image.png
Clone or Download
js-apis-getContext.md 3.80 KB
Copy Edit Raw Blame History
ester.zhou authored 9 months ago . Update docs (60021)

getContext

The getContext API enables you to obtain the context of the ability (either UIAbilityContext or ExtensionContext) on the current page.

NOTE

  • This API is supported since API version 9.
  • This API applies only to the stage model.

getContext(component?: Object):Context

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

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

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

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.

Example

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

NOTE

To avoid confusion with instances, it is recommended that you obtain a UIContext instance using the getUIContext API, and then call getContext bound to the context through the getHostContext API.

// EntryAbility.ets
import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}

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

//pages/Index.ets
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            // You are advised to use this.getUIContext().getHostContext().
            let context : Context = getContext(this) as Context
            console.info("CacheDir:" + context.cacheDir)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
other
1
https://gitee.com/openharmony/docs.git
git@gitee.com:openharmony/docs.git
openharmony
docs
docs
193b97d744efe5fa2588a62811bda5009ddf2319

Search