HarmonyOS鸿蒙Next中使用Navigation作为路由导航返回还会返回到第一个Component

HarmonyOS鸿蒙Next中使用Navigation作为路由导航返回还会返回到第一个Component

这个是主入口

```typescript
import { SplashPage } from '[@ohos](/user/ohos)/home';

[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
  [@Provide](/user/Provide)('navPathStack') navPathStack: NavPathStack = new NavPathStack();

  build() {
    Navigation(this.navPathStack) {
      SplashPage()
    }
    .mode(NavigationMode.Stack)
    .hideToolBar(true)
    .hideTitleBar(true)
    .hideBackButton(true)
    .width('100%')
    .height('100%')
  }
}

SplashPage页面

import { router } from '[@kit](/user/kit).ArkUI'

[@Component](/user/Component)
export struct SplashPage {
  timeOutId: number = 0
  [@Consume](/user/Consume)("navPathStack") pathStack: NavPathStack

  build() {
    NavDestination() {
      Image($r('app.media.bg_splash'))
        .objectFit(ImageFit.Cover)
        .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
    }
    .onReady((context: NavDestinationContext) => {

    })
  }

  onDidBuild(): void {
    setTimeout(() => {
      router.clear()
      this.pathStack.pushPathByName('loginPage', "")
    }, 1500)
  }
}

闪屏页倒计时结束进入登录页面

[@Component](/user/Component)
export struct LoginPage {
  [@Consume](/user/Consume)("navPathStack") pathStack: NavPathStack
  [@State](/user/State) message: string = 'Hello World';

  build() {
    NavDestination() {
      Row() {
        Column() {
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              this.pathStack.popToName("mainPage")
            })
        }
        .width('100%')
      }
      .height('100%')
    }
    .hideTitleBar(true)
  }
}

[@Builder](/user/Builder)
export function LoginPageBuilder() {
  LoginPage()
}

如何在登录页面禁止返回到 闪屏页面,直接退出应用


更多关于HarmonyOS鸿蒙Next中使用Navigation作为路由导航返回还会返回到第一个Component的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

可以试试使用 UIAbilityContext.terminateSelf 退出当前应用,killAllProcesses 是找出应用下面所有的进程,逐个 kill 掉。terminateSelf 是走 UIAbility 的正常生命周期,正常销毁。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-inner-application-uiabilitycontext-V5#uiabilitycontextterminateself

可以先使用 terminateSelf 关闭页面,然后在回调中执行 killAllProcesses,在后台杀掉所有进程。

try {
  this.context.terminateSelf((err: BusinessError) => {
    let applicationContext = this.context.getApplicationContext();
    applicationContext.killAllProcesses();
    if (err.code) {
      console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    console.info('terminateSelf succeed');
  });
} catch (err) { // 捕获同步的参数错误
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
}

更多关于HarmonyOS鸿蒙Next中使用Navigation作为路由导航返回还会返回到第一个Component的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,使用Navigation作为路由导航时,如果返回操作返回到第一个Component,通常是因为导航栈中只保留了初始的Component。Navigation的默认行为是保留导航栈中的历史记录,但如果在导航过程中没有正确管理栈状态,可能会导致返回到初始Component。可以通过在导航时明确指定栈管理策略,如使用router.replacerouter.push来确保导航栈的正确状态。

在HarmonyOS鸿蒙Next中,使用Navigation进行路由导航时,如果返回时仍然回到第一个Component,可能是由于Navigation的默认行为或配置问题导致的。确保你在导航时正确设置了路由栈,并检查是否有任何重置栈的操作。你可以通过Navigation.replace()Navigation.push()来管理导航栈,确保返回时不会意外回到初始页面。

回到顶部