UIAbility 及 页面间跳转
官方视频教程:https://developer.huawei.com/consumer/cn/training/course/slightMooc/C101667310940295021
视频教程里对应的文档很全,这里不多赘述了
注意
UIAbility的生命周期包括Create、Foreground、Background、Destroy四个状态,WindowStageCreate
和WindowStageDestroy
为窗口管理器(WindowStage)在UIAbility中管理UI界面功能的两个生命周期回调,从而实现UIAbility与窗口之间的弱耦合。
路由传参
IndexPage.ets
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import router from '@ohos.router';
import CommonConstants from '../common/constants/CommonConstants';
import Logger from '../common/utils/Logger';
const TAG = '[IndexPage]';
/**
* The IndexPage is the entry point of the application.
*/
@Entry
@Component
struct IndexPage {
@State message: string = CommonConstants.INDEX_MESSAGE;
@State content: string = '';
onPageShow(){
// 在这里获取
let variable = (router.getParams() as Record<string, string>)
if (variable != undefined) {
this.content = variable['content']
}
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(CommonConstants.FONT_SIZE)
.fontWeight(FontWeight.Bold)
Text(this.content)
.fontSize(CommonConstants.PARAMS_FONT_SIZE)
.opacity(CommonConstants.PARAMS_OPACITY)
Blank()
Button($r('app.string.next'))
.fontSize(CommonConstants.BUTTON_FONT_SIZE)
.width(CommonConstants.BUTTON_WIDTH)
.height(CommonConstants.BUTTON_HEIGHT)
.backgroundColor($r('app.color.button_bg'))
.onClick(() => {
router.pushUrl({
url: CommonConstants.SECOND_URL,
params: {
src: CommonConstants.SECOND_SRC_MSG
}
}).catch((error: Error) => {
Logger.info(TAG, 'IndexPage push error' + JSON.stringify(error));
});
})
}
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.LAYOUT_HEIGHT)
}
.height(CommonConstants.FULL_HEIGHT)
.backgroundColor($r('app.color.page_bg'))
}
}
SecondPage.ets
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import router from '@ohos.router';
import CommonConstants from '../common/constants/CommonConstants';
/**
* The SecondPage is the router push page.
*/
@Entry
@Component
struct SecondPage {
@State message: string = CommonConstants.SECOND_MESSAGE;
// private variable = (router.getParams() as Record<string, string>)
// @State src: string = this.variable != undefined ? this.variable[CommonConstants.SECOND_SRC_PARAM] : "mahe666";
// 原来的代码
@State src: string = (router.getParams() as Record<string, string>)[CommonConstants.SECOND_SRC_PARAM];
build() {
Row() {
Column() {
Text(this.message)
.fontSize(CommonConstants.FONT_SIZE)
.fontWeight(FontWeight.Bold)
Text(this.src)
.fontSize(CommonConstants.PARAMS_FONT_SIZE)
.opacity(CommonConstants.PARAMS_OPACITY)
Blank()
Button($r('app.string.back'))
.fontSize(CommonConstants.BUTTON_FONT_SIZE)
.width(CommonConstants.BUTTON_WIDTH)
.height(CommonConstants.BUTTON_HEIGHT)
.backgroundColor($r('app.color.button_bg'))
.onClick(() => {
router.back({
url: "pages/IndexPage",
params: {
content: "Second页面传来的数据"
}
});
})
}
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.LAYOUT_HEIGHT)
}
.height(CommonConstants.FULL_HEIGHT)
.backgroundColor($r('app.color.page_bg'))
}
}