4-3案例:UIAbility和自定义组件生命周期

This commit is contained in:
huangge1199 2024-10-25 10:24:58 +08:00
parent 02fea1b4a8
commit 2f784133e5
8 changed files with 107 additions and 15 deletions

View File

@ -139,6 +139,22 @@ export class CommonConstants {
static readonly LINK_DATA: Link[] = [ static readonly LINK_DATA: Link[] = [
new Link("第二页","pages/Second"), new Link("第二页","pages/Second"),
new Link("方法页","pages/Method"), new Link("方法页","pages/Method"),
new Link("待办页","pages/ToDoListPage") new Link("待办页","pages/ToDoListPage"),
new Link("生命周期","")
]; ];
/**
* Default fontSize
*/
static DEFAULT_FONT_SIZE: number = 30;
/**
* The text width
*/
static DEFAULT_MARGIN: number = 30;
/**
* Full the width.
*/
static FULL_WIDTH: string = '100%';
} }

View File

@ -3,39 +3,59 @@ import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI'; import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
domain: number = 0x0000;
windowStageEventFunc: (data: window.WindowStageEventType) => void = (data: window.WindowStageEventType): void => {
hilog.info(
this.domain,
'Succeeded in enabling the listener for window stage event changes. Data: %{public}',
JSON.stringify(data) ?? ''
);
}
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onCreate');
} }
onDestroy(): void { onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onDestroy');
} }
onWindowStageCreate(windowStage: window.WindowStage): void { onWindowStageCreate(windowStage: window.WindowStage): void {
// 设置WindowStage事件订阅获取/失焦、可见/不可见)
try {
windowStage.on('windowStageEvent', this.windowStageEventFunc);
} catch (exception) {
hilog.error(
this.domain,
'Failed to enable the listener for window stage event changes. Cause: %{public}',
JSON.stringify(exception) ?? ''
);
}
// Main window is created, set main page for this ability // Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => { windowStage.loadContent('pages/Index', (err) => {
if (err.code) { if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); hilog.error(this.domain, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return; return;
} }
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); hilog.info(this.domain, 'testTag', 'Succeeded in loading the content.');
}); });
} }
onWindowStageDestroy(): void { onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources // Main window is destroyed, release UI related resources
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
} }
onForeground(): void { onForeground(): void {
// Ability has brought to foreground // Ability has brought to foreground
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onForeground');
} }
onBackground(): void { onBackground(): void {
// Ability has back to background // Ability has back to background
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onBackground');
} }
} }

View File

@ -8,7 +8,7 @@ import DataModel from '../viewmodel/DataModel';
@Entry @Entry
@Component @Component
struct Index { struct Index {
@State message: string = 'Hello World' @State message: string = 'HarmonyOS第一课学习'
private linkData: Link[] = []; private linkData: Link[] = [];
@ -20,22 +20,22 @@ struct Index {
Row() { Row() {
Column() { Column() {
Text(this.message) Text(this.message)
.fontSize(50) .fontSize(30)
.fontWeight(FontWeight.Bold) .fontWeight(FontWeight.Bold)
// 添加按钮,以响应用户点击 // 添加按钮,以响应用户点击
ForEach(this.linkData, (link: Link) => { ForEach(this.linkData, (link: Link) => {
Button() { Button() {
Text(link.name) Text(link.name)
.fontSize(30) .fontSize(24)
.fontWeight(FontWeight.Bold) .fontWeight(FontWeight.Bold)
} }
.type(ButtonType.Capsule) .type(ButtonType.Capsule)
.margin({ .margin({
top: 20 top: 10
}) })
.backgroundColor('#0D9FFB') .backgroundColor('#0D9FFB')
.width('40%') .width('80%')
.height('5%') .height('5%')
.onClick(() => { .onClick(() => {
console.info(`Succeeded in clicking the 'Next' button.`) console.info(`Succeeded in clicking the 'Next' button.`)

View File

@ -0,0 +1,43 @@
import Logger from '../common/utils/Logger';
import { CommonConstants } from '../common/constants/Constants';
@Entry
@Component
struct LifeCyclePage {
@State textColor: Color = Color.Black;
aboutToAppear() {
this.textColor = Color.Blue;
Logger.info('[LifeCyclePage] LifeCyclePage aboutToAppear');
}
onPageShow() {
this.textColor = Color.Brown;
Logger.info('[LifeCyclePage] LifeCyclePage onPageShow');
}
onPageHide() {
Logger.info('[LifeCyclePage] LifeCyclePage onPageHide');
}
onBackPress() {
this.textColor = Color.Red;
Logger.info('[LifeCyclePage] LifeCyclePage onBackPress');
return false;
}
aboutToDisappear() {
Logger.info('[LifeCyclePage] LifeCyclePage aboutToDisappear');
}
build() {
Column() {
Text($r('app.string.hello_message'))
.fontSize(CommonConstants.DEFAULT_FONT_SIZE)
.fontColor(this.textColor)
.margin(CommonConstants.DEFAULT_MARGIN)
.fontWeight(FontWeight.Bold)
}
.width(CommonConstants.FULL_WIDTH)
}
}

View File

@ -59,6 +59,10 @@
{ {
"name": "page_title", "name": "page_title",
"value": "待办" "value": "待办"
},
{
"name": "hello_message",
"value": "Hello World"
} }
] ]
} }

View File

@ -3,6 +3,7 @@
"pages/Index", "pages/Index",
"pages/Second", "pages/Second",
"pages/Method", "pages/Method",
"pages/ToDoListPage" "pages/ToDoListPage",
"pages/LifeCyclePage"
] ]
} }

View File

@ -59,6 +59,10 @@
{ {
"name": "page_title", "name": "page_title",
"value": "ToDo" "value": "ToDo"
},
{
"name": "hello_message",
"value": "Hello World"
} }
] ]
} }

View File

@ -59,6 +59,10 @@
{ {
"name": "page_title", "name": "page_title",
"value": "待办" "value": "待办"
},
{
"name": "hello_message",
"value": "你好世界"
} }
] ]
} }