navigation实现dialog偶现pop无法关闭 HarmonyOS 鸿蒙Next

navigation实现dialog偶现pop无法关闭 HarmonyOS 鸿蒙Next 使用navigation实现dialog,相同的dialog在某个页面pop无法关闭,且pop携带的值已经传递到上一个页面了,都是在hsp中使用

工程机版本:5.0.0.60
DevEco Studio版本:5.0.3.800

以下为dialog代码

import { DebugManager } from 'debugMgr'
import { DisplayUtils } from 'libCommon'
import { IWebViewService, WebViewPageParams } from 'moduleApi'
import { YooseeModuleManager } from 'moduleMgr'

export interface AgreementDialogPopInfo {
    /**
     * 是否同意了隐私协议
     */
    agree: boolean
}

@Builder
export function AgreementDialogBuilder() {
    AgreementDialog()
}

@Component
struct AgreementDialog {
    private pageInfos: NavPathStack = new NavPathStack()

    build() {
        NavDestination() {
            Column() {
                Stack() {
                    Column() {
                        Text() {
                            Span($r('app.string.before_user_pro'))
                                .fontSize($r('app.float.font_15'))
                                .fontColor($r('app.color.color_ff333333'))

                            Span($r('app.string.user_policy'))
                                .fontSize($r('app.float.font_15'))
                                .fontColor($r('app.color.color_ff333333'))
                                .fontWeight(FontWeight.Bold)
                                .onClick(() => {
                                    this.goToWeb(false)
                                })

                            Span($r('app.string.private_policy'))
                                .fontSize($r('app.float.font_15'))
                                .fontColor($r('app.color.color_ff333333'))
                                .fontWeight(FontWeight.Bold)
                                .onClick(() => {
                                    this.goToWeb(true)
                                })
                        }.padding({
                            top: $r('app.float.vp_70'),
                            left: $r('app.float.vp_20'),
                            right: $r('app.float.vp_20')
                        })

                        Text($r('app.string.AA2505'))
                            .fontSize($r('app.float.font_16'))
                            .textAlign(TextAlign.Center)
                            .fontColor($r('app.color.color_ff333333'))
                            .backgroundColor($r('app.color.color_white'))
                            .borderWidth($r('app.float.vp_0_5'))
                            .borderColor($r("app.color.color_ffe5e5e5"))
                            .width('90%')
                            .height($r('app.float.vp_46'))
                            .borderRadius($r('app.float.vp_23'))
                            .margin({
                                top: $r('app.float.vp_40'),
                                bottom: $r('app.float.vp_6')
                            })
                            .onClick(() => {
                                this.pop(false)
                            })

                        Text($r('app.string.AA2506'))
                            .fontSize($r('app.float.font_16'))
                            .textAlign(TextAlign.Center)
                            .fontColor($r('app.color.color_white'))
                            .backgroundColor($r('app.color.color_ff2976ff'))
                            .width('90%')
                            .height($r('app.float.vp_46'))
                            .borderRadius($r('app.float.vp_23'))
                            .margin({
                                top: $r('app.float.vp_16'),
                                bottom: $r('app.float.vp_20')
                            })
                            .onClick(() => {
                                this.pop(true)
                            })
                    }.backgroundColor($r('app.color.color_white'))
                    .borderRadius($r('app.float.vp_16'))
                    .margin({
                        top: $r('app.float.vp_35'),
                    })

                    Image($r('app.media.ic_account_login_user_agreement'))
                        .size({
                            width: $r('app.float.vp_70'),
                            height: $r('app.float.vp_70')
                        })
                }
                .alignContent(Alignment.Top)
                .width(px2vp(DisplayUtils.getScreenWidth()) * 0.9)
            }.width('100%')
            .height('100%')
            .backgroundColor($r('app.color.color_1A000000'))
            .justifyContent(FlexAlign.Center)
        }.hideTitleBar(true)
        .mode(NavDestinationMode.DIALOG)
        .onReady((context: NavDestinationContext) => {
            this.pageInfos = context.pathStack
        })
    }

    private pop(agree: boolean) {
        const popInfo: AgreementDialogPopInfo = {
            agree: agree
        }
        this.pageInfos.pop(popInfo)
    }

    private async goToWeb(privacy: boolean) {
        const webViewService =
            await YooseeModuleManager.getInstance().getModuleService<IWebViewService>('webview_module')
        const webPageParams: WebViewPageParams = {
            url: privacy ? DebugManager.getInstance().getWebUrl().getPrivacyUrl() :
            DebugManager.getInstance().getWebUrl().getProtocolUrl()
        }
        webViewService?.startWebWithUrl(webPageParams)
    }
}

更多关于navigation实现dialog偶现pop无法关闭 HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

可以在NavDestination中加上onBackPressed重写返回键逻辑:

接口链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V14/ts-basic-components-navdestination-V14#onbackpressed10

.onBackPressed(() => {
  this.pageInfo.pop({number: 1}) // 弹出路由栈栈顶元素。
  return true
})

更多关于navigation实现dialog偶现pop无法关闭 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,navigation实现dialog时偶现pop无法关闭的问题,可能涉及以下几个原因:

  1. 生命周期管理问题dialog的生命周期可能与navigation的生命周期未完全同步,导致pop操作未正确触发dialog的关闭。需要检查dialog的创建和销毁逻辑是否与navigationpop操作一致。

  2. 状态管理异常dialog的状态可能在pop操作时未被正确重置或清除,导致界面无法更新。需要检查dialog的状态管理逻辑,确保在pop操作时状态能够被正确重置。

  3. 事件传递问题pop操作可能未正确传递到dialog,导致dialog未接收到关闭指令。需要检查navigationdialog之间的事件传递机制,确保pop操作能够正确传递到dialog

  4. UI线程阻塞dialog的关闭操作可能被UI线程阻塞,导致pop操作无法立即生效。需要检查dialog的关闭操作是否在UI线程中执行,并确保没有阻塞操作。

  5. 系统资源限制:在某些情况下,系统资源限制可能导致pop操作无法正常执行。需要检查系统资源使用情况,确保pop操作有足够的资源支持。

综上所述,navigation实现dialog偶现pop无法关闭的问题可能涉及生命周期管理、状态管理、事件传递、UI线程阻塞或系统资源限制等方面。需要针对具体问题进行分析和排查。

回到顶部