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[] = [
new Link("第二页","pages/Second"),
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';
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 {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onDestroy');
}
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
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
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;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
hilog.info(this.domain, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// 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 {
// Ability has brought to foreground
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
hilog.info(this.domain, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// 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
@Component
struct Index {
@State message: string = 'Hello World'
@State message: string = 'HarmonyOS第一课学习'
private linkData: Link[] = [];
@ -20,22 +20,22 @@ struct Index {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontSize(30)
.fontWeight(FontWeight.Bold)
// 添加按钮,以响应用户点击
ForEach(this.linkData, (link: Link) => {
Button() {
Text(link.name)
.fontSize(30)
.fontSize(24)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
top: 10
})
.backgroundColor('#0D9FFB')
.width('40%')
.width('80%')
.height('5%')
.onClick(() => {
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",
"value": "待办"
},
{
"name": "hello_message",
"value": "Hello World"
}
]
}

View File

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

View File

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

View File

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