繁体   English   中英

使用jetpack导航组件从一个底页导航到另一个底页时如何弹出底页对话框

[英]How to pop the bottomsheet dialog when navigating from one bottomsheet to another using jetpack navigation component

我有一个带有片段和 2 个底部表对话框片段的导航堆栈。 这就是我的导航图的样子,

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/football_home"
    android:id="@+id/home">

    <fragment
        android:id="@+id/football_home"
        android:name="com.football.mib.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/launch_otl_link_prompt"
            app:destination="@+id/initial_flow"/>
    </fragment>

    <dialog android:id="@+id/initial_flow"
        android:name="com.football.mib.ui.consent.LinkPromptFragment"
        android:label="@string/title_initial_flow"
        tools:layout="@layout/fragment_link_prompt">

        <action android:id="@+id/activate_game"
            app:popUpTo="@id/game_activated"
            app:popUpToInclusive="true"
            app:destination="@id/game_activated"/>

    </dialog>

    <dialog android:id="@+id/game_activated"
        android:name="com.football.mib.ui.consent.GameActivatedFragment"
        android:label="@string/game_activated"
        tools:layout="@layout/fragment_game_activated" />

</navigation>

我可以轻松地在片段和对话框之间导航,但是在导航到第二个时我需要弹出前一个底页。 我曾尝试使用app:popUpToapp:popUpToInclusive但第一个对话框initial_flow在后台堆栈中仍然可用,并且关闭第二个对话框game_activated会将其再次置于顶部。

当导航到第二个对话框无济于事时,我还尝试通过 navOptions 以编程方式调用 popUp,例如

binding.activateOneTap.setOnClickListener {
        val navOptions = navOptions {
            popUpTo(R.id.game_activated) {
                inclusive = true
            }
        }
        findNavController().navigate(R.id.activate_game, null, navOptions)
    }

我正在使用导航组件 2.3.4。

任何提示或替代方案?

发现问题,问题是因为在app:popUpTo中使用下一个对话框 id 而不是当前对话框。 更新 id 解决了这个问题。

<action android:id="@+id/activate_game"
            app:popUpTo="@id/initial_flow"
            app:popUpToInclusive="true"
            app:destination="@id/game_activated"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM