我在新的Android应用上使用了导航组件,但我不知道该怎么做
首先,我有MainActivity,那里有main_navigation_graph
主要活动NavHostFragment
<fragment
android:id="@+id/main_navigation_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_navigation_graph"
app:defaultNavHost="true"/>
在main_navigation_graph里面有3个片段
这里一切都很好。问题是当我到达最后一个片段时,因为在这个片段上,我想基于BottomNavigationView输入(目前)显示一些子片段(在新的NavHostFragment内)
LastFragment:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<fragment
android:id="@+id/dash_board_navigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/dash_board_bottom_navigation"
app:navGraph="@navigation/dash_board_navigation_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/dash_board_bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:menu="@menu/main_menu_navigation"
app:labelVisibilityMode="labeled"
android:background="@color/main_menu_background"
app:itemIconTint="@color/main_menu_item_color"
app:itemTextColor="@color/main_menu_item_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
dash_board_navigation_graph
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dash_board_navigation_graph"
app:startDestination="@id/destination_home_fragment">
<action
android:id="@+id/dash_board_action1"
app:destination="@id/destination_fragment1"/>
<action
android:id="@+id/dash_board_action2"
app:destination="@id/destination_fragment2"/>
<action
android:id="@+id/dash_board_action3"
app:destination="@id/destination_fragment3"/>
<action
android:id="@+id/dash_board_action4"
app:destination="@id/destination_fragment4"/>
<fragment
android:id="@+id/destination_fragment1"
android:name="com.....Fragment1">
</fragment>
<fragment
android:id="@+id/destination_fragment2"
android:name="com.....Fragment2">
</fragment>
<fragment
android:id="@+id/destination_fragment3"
android:name="com.....Fragment3">
</fragment>
<fragment
android:id="@+id/destination_fragment4"
android:name="com.....Fragment4">
</fragment>
当我从上一个片段触发以下操作时:
findNavController().navigate(R.id.dash_board_action1/2/3)
我收到此异常:
java.lang.IllegalArgumentException: navigation destination com.asperitass.mobile:id/dash_board_home_action is unknown to this NavController
我的问题是:我的方法正确吗?还是有可能(具有多个级别的导航)?还是应该使用childFragmentManager
处理此子导航,并在片段中填充FrameLayout?
答案 0 :(得分:0)
据我所知,您正在嵌套两个NavHostFragments,因为您希望在诸如lastFragment中的登录之类的流程之后仅具有bottomNav,
据我了解,问题出在findNavController().navigate(R.id.dash_board_action1/2/3)
中。由于您有两个NavHostFragments,因此您还将有两个对应的NavControllers。
默认的Navigation.findNavController()
将使您获得最高的NavController,但是要在子navGraph中导航,您需要可以通过
val fragmentContainer = view.findViewById<View>(R.id.dash_board_navigation)
navController = Navigation.findNavController(fragmentContainer)
有关更多详细信息,请查看这个非常好的Stackoverflow答案https://stackoverflow.com/a/51500083/6269691