首页 > 解决方案 > Android Jetpack Navigation 组件问题,将启动片段作为根目的地

问题描述

假设我们有 3 个带有向上按钮功能的工具栏片段

SplashFragment -> FragmentA -> FragmentB 

这是应用程序的预期流程。当用户按下返回按钮时到达 FragmentB,他应该转到 FragmentA,然后再次按下返回按钮,应用程序应该退出。我们如何使用导航组件来实现这种行为?我尝试使用标签app:popUpTo内部来做到这一点action,不知何故我设法使它适用于硬件后退按钮,但向上按钮的行为没有改变。

在这方面需要帮助。

标签: androidkotlinandroid-navigationandroid-jetpackandroid-architecture-navigation

解决方案


警告:这在 Navigation alpha08 上已弃用,并且链接是当前的解决方案。

这可能是你想要的答案。利用app:clearTask="true"

<fragment
    android:id="@+id/splashFragment"
    android:name="xxx.SplashFragment"
    android:label="fragment_splash"
    tools:layout="@layout/fragment_splash">
    <action
        android:id="@+id/action_splashFragment_to_mainFragment"
        app:destination="@id/mainFragment"
        app:enterAnim="@anim/anim_right_in"
        app:exitAnim="@anim/anim_left_out"
        app:popEnterAnim="@anim/anim_left_in"
        app:popExitAnim="@anim/anim_right_out"
        app:clearTask="true"/>

导航架构组件 - 启动画面


推荐阅读