harmony 鸿蒙UIAbility Usage

  • 2023-02-03
  • 浏览 (722)

UIAbility Usage

When using the UIAbility component, you must specify a startup page and obtain the context, UIAbilityContext.

Specifying the Startup Page of UIAbility

You can use loadContent() of WindowStage 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.

import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created. Set a main page for this ability.
    windowStage.loadContent('pages/Index', (err, data) => {
      // ...
    });
  }

  // ...
}

NOTE

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.

Obtaining the Context of UIAbility

The UIAbility class has its own context, which is an instance of the UIAbilityContext 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().

  • You can use this.context to obtain the context of a UIAbility instance.
  import UIAbility from '@ohos.app.ability.UIAbility';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  import Want from '@ohos.app.ability.Want';
  
  export default class EntryAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
      // Obtain the context of the UIAbility instance.
      let context = this.context;
      ...
    }
  }
  • Import the context module and define the context variable in the component.
  import common from '@ohos.app.ability.common';
  import Want from '@ohos.app.ability.Want';
  
  @Entry
  @Component
  struct Index {
    private context = getContext(this) as common.UIAbilityContext;
  
    startAbilityTest() {
      let want: Want = {
        // Want parameter information.
      };
      this.context.startAbility(want);
    }
  
    // Page display.
    build() {
      ...
    }
  }

You can also define variables after importing the context module but before using UIAbilityContext.

  import common from '@ohos.app.ability.common';
  import Want from '@ohos.app.ability.Want';
  
  @Entry
  @Component
  struct Index {
  
    startAbilityTest() {
      let context = getContext(this) as common.UIAbilityContext;
      let want: Want = {
        // Want parameter information.
      };
      context.startAbility(want);
    }
  
    // Page display.
    build() {
      ...
    }
  }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞