我正在尝试新的Navigation Architecture Component,但不知道如何执行此操作:

我有1个 Activity (MainActivity)+ 3个片段:

  • SplashFragment(主页)
  • MainFragment
  • SignUpFragment

  • 我想使用SplashFragment来确定是否应该导航到MainFragment或SignUpFragment,但是一旦到达其中两个,您就无法弹出SplashFragment。如何使用新的导航组件来做到这一点?

    我在调用popBackStack之前和之后尝试了navigate(R.id.action_xxx),但是它们都不起作用(这很有意义:在弹出之前没有任何内容;在关闭刚刚添加的片段之后)。这是否意味着唯一的方法就是重写onBackPress来拦截它,并确保在那种情况下navigateUp不被调用?

    谢谢!

    最佳答案

    首先,将app:popUpTo='your_nav_graph_id'app:popUpToInclusive="true"属性添加到action标签。

    <fragment
        android:id="@+id/signInFragment"
        android:name="com.glee.incog2.android.fragment.SignInFragment"
        android:label="fragment_sign_in"
        tools:layout="@layout/fragment_sign_in" >
        <action
            android:id="@+id/action_signInFragment_to_usersFragment"
            app:destination="@id/usersFragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/main_nav_graph"
            app:popUpToInclusive="true" />
    </fragment>
    

    其次,使用上述操作作为参数导航到目的地。
    findNavController(fragment).navigate(SignInFragmentDirections.actionSignInFragmentToUserNameFragment())
    

    注意:如果使用navigate(@IdRes int resId)方法导航,则不会获得所需的结果。因此,我使用了navigate(@NonNull NavDirections directions)方法。

    关于android - 如何使用新的导航体系结构组件为某些 fragment 禁用导航中的UP?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50336112/

    10-11 22:15
    查看更多