HarmonyOS/OpenHarmony application development - Stage model UIAbility component use (3)

Basic usage of UIAbility components The
basic usage of UIAbility components includes: specifying the startup page of UIAbility and obtaining the context UIAbilityContext of UIAbility.
1. Specify the startup page of UIAbility
During the startup process of UIAbility in the application, you need to specify the startup page, otherwise the application will cause a white screen because no default page is loaded after the application starts. You can set the startup page through the loadContent() method of the WindowStage object in the onWindowStageCreate() life cycle callback of UIAbility.

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 main page for this ability
        windowStage.loadContent('pages/Index', (err, data) => {
            // ...
        });
    }

    // ...
}

Note: In the UIAbility created in DevEco Studio, the UIAbility instance will load the Index page by default, and replace the Index page path with the required page path as needed.

2. Get the context information of UIAbility
The UIAbility class has its own context information, which is an instance of the UIAbilityContext class, and the UIAbilityContext class has attributes such as abilityInfo and currentHapModuleInfo. Through UIAbilityContext, you can obtain UIAbility related configuration information, such as package code path, Bundle name, Ability name, and the environment status required by the application, as well as methods for operating UIAbility instances (such as startAbility(), connectServiceExtensionAbility(), terminateSelf) ()wait).
In UIAbility, the context information of the UIAbility instance can be obtained through this.context.

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

export default class EntryAbility extends UIAbility {
    onCreate(want, launchParam) {
        // 获取UIAbility实例的上下文
        let context = this.context;

        // ...
    }
}

Obtaining the context information of the UIAbility instance in the page includes importing the dependent resource context module and defining a context variable in the component.

import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {
  private context = getContext(this) as common.UIAbilityContext;

  startAbilityTest() {
    let want = {
      // Want参数信息
    };
    this.context.startAbility(want);
  }

  // 页面展示
  build() {
    // ...
  }
}

You can also define variables after importing the dependent resource context module and before using UIAbilityContext.

import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {

  startAbilityTest() {
    let context = getContext(this) as common.UIAbilityContext;
    let want = {
      // Want参数信息
    };
    context.startAbility(want);
  }

  // 页面展示
  build() {
    // ...
  }
}

Guess you like

Origin blog.csdn.net/weixin_69135651/article/details/131581594