繁体   English   中英

Android Jetpack 导航组件问题,以启动片段为根目标

[英]Android Jetpack Navigation component issue with splash fragment as root destination

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

SplashFragment -> FragmentA -> FragmentB 

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

在这方面需要帮助。

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

导航架构组件 - 启动画面

您可以尝试这样做:

App 启动 Fragment-A 后,清除 backstack ie; SplashFragment ,现在当用户向前导航时,一切都相同,但在返回 Fragment-A 后,如果他点击后退按钮应用程序退出。

希望这可以帮助!

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

希望这可以帮助。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM