HarmonyOS鸿蒙Next中使用Navigation做路由跳转,怎么实现从第一个页面跳转到第二个页面后,返回直接退出App
HarmonyOS鸿蒙Next中使用Navigation做路由跳转,怎么实现从第一个页面跳转到第二个页面后,返回直接退出App 从根页面加载数据后跳转到首页,但返回会回到根页面,该怎么处理,使用router.replace可以实现,想使用Navigation没找到方法
this.pageStack.replacePath({ name: 'controlPage' })
根页面
Navigation(this.pageStack) {
Column() {
LoadingProgress().width(80)
Text('加载中...')
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
}
controlPage页
NavDestination() {
Text('首页')
}
更多关于HarmonyOS鸿蒙Next中使用Navigation做路由跳转,怎么实现从第一个页面跳转到第二个页面后,返回直接退出App的实战教程也可以访问 https://www.itying.com/category-93-b0.html
Navigation启动时,通过this.pathStack.pushPathByName
直接跳转到MainPage界面;
添加路由拦截,当路由目标页面名称为navBar(Navigation首页名字)时,就跳转到MainPage界面;
设置Navigation属性hideNavBar
为true,隐藏返回导航栏。
具体代码实现如下:
- MainPage代码:
import { router } from '@kit.ArkUI';
@Component
struct MainPage {
pageName: string = '';
pathStack: NavPathStack | undefined = undefined;
build() {
NavDestination() {
Column() {
Text('this is MainPage').fontSize(24)
Button(`push Page`).width('80%').margin({ top: 10, bottom: 10 })
.onClick(() => {
this.pathStack?.pushPathByName('Page', '');
})
Button(`pop`).width('80%').margin({ top: 10, bottom: 10 })
.onClick(() => {
this.pathStack?.pop();
})
}
}.title('MainPage')
.onReady((context: NavDestinationContext) => {
this.pageName = context.pathInfo.name;
this.pathStack = context.pathStack;
})
.onBackPressed(): boolean {
router.back();
return true;
}
}
}
- Page代码:
@Component
struct Page {
pageName: string = '';
pathStack: NavPathStack | undefined = undefined;
build() {
NavDestination() {
Column() {
Text('this is Page').fontSize(24)
Button(`pop`).width('80%').margin({ top: 10, bottom: 10 })
.onClick(() => {
this.pathStack?.pop();
})
}
}.title('Page')
.onReady((context: NavDestinationContext) => {
this.pageName = context.pathInfo.name;
this.pathStack = context.pathStack;
})
}
}
- Index代码(即启动页):
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
struct Index {
pathStack: NavPathStack = new NavPathStack();
@State hideNavBar: boolean = false
aboutToAppear(): void {
setTimeout(() => {
this.pathStack.pushPathByName('MainPage', undefined, false);
this.hideNavBar = true
// 添加路由拦截功能
this.pathStack.setInterception({
willShow: (from: NavDestinationContext | NavBar, to: NavDestinationContext | NavBar,
operation: NavigationOperation, isAnimated: boolean) => {
// 如果返回到首页,就跳转为MainPage页面
if (to == 'navBar') {
promptAction.showToast({ message: '无法返回到首页' })
this.pathStack.pushPathByName('MainPage', undefined, false);
}
}
})
}, 2000)
}
@Builder
pagesMap(name: string, param: number) {
if (name == 'MainPage') {
MainPage()
} else if (name == 'Page') {
Page()
}
}
build() {
Navigation(this.pathStack) {
Text('欢迎进入APP').fontSize(30)
}.title('启动页')
.hideNavBar(this.hideNavBar)
.navDestination(this.pagesMap)
}
}
更多关于HarmonyOS鸿蒙Next中使用Navigation做路由跳转,怎么实现从第一个页面跳转到第二个页面后,返回直接退出App的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
UIAbilityContext.terminateSelf ; 退出应用
this.pageStack.clear; 清空栈,然后用push返回到首页就行了
在HarmonyOS鸿蒙Next中,使用Navigation
进行路由跳转时,若要从第一个页面跳转到第二个页面后,返回直接退出App,可以在第二个页面中重写onBackPress
方法,并调用AbilityContext
的terminateSelf
方法。具体代码如下:
@Override
public boolean onBackPress() {
getContext().terminateSelf();
return true; // 返回true表示已处理返回事件
}
这样,当用户从第二个页面返回时,App将直接退出。