如何使用新的导航体系结构组件为某些片段禁用导航中的UP? [英] How to disable UP in Navigation for some fragment with the new Navigation Architecture Component?

查看:63
本文介绍了如何使用新的导航体系结构组件为某些片段禁用导航中的UP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试新的导航架构组件,我可以不知道该怎么做:

I am trying out the new Navigation Architecture Component, and I can't figure out how to do this:

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

I have 1 Activity (MainActivity) + 3 Fragments:

  • SplashFragment(主页)
  • MainFragment
  • SignUpFragment

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

I would like to use SplashFragment to determine if I should navigate to MainFragment or SignUpFragment, but once it reaches either of those 2, you should not be able to pop back to SplashFragment. How can I do that with the new navigation component?

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

I tried popBackStack before and after calling navigate(R.id.action_xxx), but neither of them work (which make sense: before it has nothing to pop; after it just closes the fragment that just got added). Does that mean the only way to do that is to override onBackPress to intercept it and make sure navigateUp does not get call in those cases?

谢谢!

推荐答案

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

First, add attributes app:popUpTo='your_nav_graph_id' and app:popUpToInclusive="true" to the action tag.

<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>

第二,使用上述操作作为参数,导航到目的地.

Second, navigate to the destination, using the above action as parameter.

findNavController(fragment).navigate(SignInFragmentDirections.actionSignInFragmentToUserNameFragment())

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

NOTE: If you navigate using method navigate(@IdRes int resId), you won't get the desired result. Hence, I used method navigate(@NonNull NavDirections directions).

这篇关于如何使用新的导航体系结构组件为某些片段禁用导航中的UP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆