Android Jetpack Navigation组件问题,飞溅片段为根目标

时间:2018-09-27 12:54:26

标签: android kotlin android-navigation android-jetpack android-architecture-navigation

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

SplashFragment -> FragmentA -> FragmentB 

这是应用程序的预期流程。当用户按下后退按钮到达FragmentB时,他应转到FragmentA,然后再次按下后退应用程序。 我们如何使用导航组件实现此行为? 我尝试使用app:popUpTo标签内的action来执行此操作,以某种方式设法使它适用于硬件后退按钮,但向上按钮的行为没有改变。

需要帮助。

4 个答案:

答案 0 :(得分:1)

警告:导航alpha08不推荐使用此方法,而link是当前解决方案。

这可能是您想要的答案。 使用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"/>

Navigation Architecture Component - Splash screen

答案 1 :(得分:0)

您可以尝试通过以下方式进行操作:

在App启动Fragment-A之后清除后栈,即SplashFragment,现在,当用户向前导航时,一切相同,但是返回到Fragment-A之后,如果他单击“后退”按钮,则应用程序退出。

希望这会有所帮助!

答案 2 :(得分:0)

我也有类似的流程,有效的方法是有效的解决方案,我相信将FragmentA作为导航图的起点(在午餐时总是放在后台)那么您想(可能有条件地)导航到SplashFragment的{​​{1}}(或生命周期的早期)中的onStart,最后,当您希望SplashFragment消失时,您使用FragmentA弹出片段。那么只有片段A和片段B会保留在您的堆栈中,并且返回片段将按预期工作。

希望这会有所帮助。

答案 3 :(得分:0)

这是我的解决方案。 fragmentA->fragmentB->fragmentc 在背面,按钮从 FragmentC 按下到 FragmentB,然后再次按下,您想退出表单应用程序。将此行添加到 FragmentA 操作中。

app:launchSingleTop="true"
app:popUpTo="@+id/fragmentA"
app:popUpToInclusive="true"