【问题标题】:How to pop the bottomsheet dialog when navigating from one bottomsheet to another using jetpack navigation component使用jetpack导航组件从一个底页导航到另一个底页时如何弹出底页对话框
【发布时间】:2022-01-13 04:02:44
【问题描述】:

我有一个带有片段和 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。

任何提示或替代方法?

【问题讨论】:

    标签: android android-jetpack-navigation bottomsheetdialogfragment


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2022-01-15
      相关资源
      最近更新 更多