Android Jetpack导航组件问题,将splash片段作为根目标

问题描述 投票:0回答:3

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

SplashFragment -> FragmentA -> FragmentB 

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

需要帮助。

android kotlin android-navigation android-jetpack android-architecture-navigation
3个回答
1
投票

警告:这在Navigation 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


0
投票

您可以尝试这样做:

在应用程序启动Fragment-A之后清除了backstack,即; SplashFragment,现在当用户向前导航时,所有内容都相同但在返回Fragment-A后,如果他回击按钮该应用程序退出。

希望这可以帮助!


0
投票

我有一个类似的流程,什么有效,我相信是一个干净的解决方案是让您的FragmentA作为导航图的起点(在午餐时总是放在靠背的顶部)然后你想要导航(也许有条件地)你的SplashFragmentonStart(或生命周期的早期)的FragmentA,最后当你希望你的SplashFragment消失时,你使用findNavController().popBackStack()弹出片段。那么只有片段A和B将保留在你的堆栈上,然后返回将按预期工作。

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.