Android导航组件清空堆栈

2023-12-28

我已导航至对话框片段 from 抽屉式导航连接到 NavController。但是,当我导航到已设置 popUpTo 和包含对话框片段的另一个目的地时,它不会清除堆栈。如何清除堆栈?

AM 从 LogoutDialog 调用此方法

findNavController().navigate(R.id.action_logoutDialog_to_auth_navigation)

我正在使用片段并且仅使用一项活动。

这是我的导航图的一部分

 <fragment
        android:id="@+id/homeFragment"
        android:name="io.ui.home.HomeFragment"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_auth_navigation"
            app:destination="@id/auth_navigation"
            app:popUpTo="@id/homeFragment"
            app:popUpToInclusive="true" />
        <argument
            android:name="fromLogin"
            app:argType="boolean"
            android:defaultValue="false" />
    </fragment>

    <dialog
        android:id="@+id/logoutDialog"
        android:name="io.ui.dialog.LogoutDialog"
        android:label="LogoutDialog" >
        <action
            android:id="@+id/action_logoutDialog_to_auth_navigation"
            app:destination="@id/auth_navigation"
            app:popUpTo="@id/logoutDialog"
            app:popUpToInclusive="true" />
    </dialog>

    <!-- auth navigation graph -->
    <navigation
        android:id="@+id/auth_navigation"
        app:startDestination="@id/loginFragment">
        <fragment
            android:id="@+id/loginFragment"
            android:name="io.ui.login.LoginFragment"
            android:label="fragment_login"
            tools:layout="@layout/fragment_login">
            <action
                android:id="@+id/action_loginFragment_to_OTPFragment"
                app:destination="@id/OTPFragment"
                app:popUpTo="@id/loginFragment"
                app:popUpToInclusive="true" />
            <action
                android:id="@+id/action_loginFragment_to_homeFragment"
                app:destination="@id/homeFragment" />
            <action
                android:id="@+id/action_loginFragment_to_resetPasswordFragment"
                app:destination="@id/resetPasswordFragment" />
        </fragment>

        <fragment
            android:id="@+id/OTPFragment"
            android:name="io.ui.login.OTPFragment"
            android:label="fragment_otp"
            tools:layout="@layout/fragment_otp">
            <action
                android:id="@+id/action_OTPFragment_to_homeFragment"
                app:destination="@id/homeFragment"
                app:popUpTo="@id/OTPFragment"
                app:popUpToInclusive="true" />
            <action
                android:id="@+id/action_OTPFragment_to_loginFragment"
                app:destination="@id/loginFragment"
                app:popUpTo="@id/OTPFragment"
                app:popUpToInclusive="true" />
        </fragment>
        <fragment
            android:id="@+id/resetPasswordFragment"
            android:name="io.ui.profile.PasswordFragment"
            android:label="fragment_reset_password"
            tools:layout="@layout/fragment_password" >
            <argument
                android:name="isReset"
                app:argType="boolean"
                android:defaultValue="false" />
        </fragment>

    </navigation>

您正在寻找这些属性来投入您的行动中。

  • The popUpTo操作的属性在导航之前将返回堆栈“弹出”到给定的目的地。 (目的地将从后堆栈中删除。)

  • If the popUpToInclusive属性为 false 或未设置,popUpTo删除指定目标之前的目标,但将指定目标保留在返回堆栈中。

  • If popUpToInclusive设置为 true 时,popUpTo属性从返回堆栈中删除直到给定目的地(包括给定目的地)的所有目的地。

  • If popUpToInclusive是真的并且popUpTo设置为应用程序的起始位置,该操作将从返回堆栈中删除所有应用程序目标。 “后退”按钮可让用户完全退出应用程序。


编辑:我还添加了这个非常棒的视频,它用示例进行了解释https://www.youtube.com/watch?v=mLfWvSGG5c8 https://www.youtube.com/watch?v=mLfWvSGG5c8

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android导航组件清空堆栈 的相关文章

随机推荐