如何使用Jetpack Navigation组件在从一个BottomSheet导航到另一个BottomSheet时弹出底部对话框

3

我有一个包含一个片段和两个bottomsheetdialogfragment的导航栈。这是我的导航图看起来像什么:

<?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。有什么提示或替代方案吗?
1个回答

1

找到了问题,问题是因为在app:popUpTo中使用了下一个对话框的ID而不是当前对话框的ID。更新ID解决了问题。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接