Android 是否可以有多个 nav_graph 文件?
Android Is it possible to have multiple nav_graph files?
所以我在使用 Jetpack navigation
并且片段的数量不断增加。
我们可以在不同的导航图中分离片段,如本文档所述
然后我尝试将不同的导航图放在不同的文件中,因为这样感觉文件更有组织性和可读性,但是当我尝试导航到不同的 nav_graph 文件时出现以下错误。
nav_graph_start.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_start"
app:startDestination="@id/splashScreen"
tools:ignore="UnusedNavigation">
<fragment
android:id="@+id/splashScreen"
android:name="com.timetoface.android.splash.SplashFragment"
android:label="Login Fragment"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splash_to_login"
app:destination="@id/nav_graph_auth"
/>
<action
android:id="@+id/action_splash_to_home"
app:destination="@id/nav_graph_home"
/>
</fragment>
</navigation>
nav_graph_auth.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_auth"
app:startDestination="@id/emailLoginScreen"
tools:ignore="UnusedNavigation">
................................
</navigation>
nav_graph_home.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_home"
app:startDestination="@id/emailLoginScreen"
tools:ignore="UnusedNavigation">
................................
</navigation>
navigation destination com.app.android:id/nav_graph_home
referenced from action com.app.android:id/action_splash_to_home
is unknown to this NavController
所以,
多个导航图文件还不支持吗?
我是否遗漏了一些我应该改变的东西?
首先你可以使用include。看看this
示例:first_graph.xml
<include app:graph="@navigation/second_graph" />
然后将操作设置为包含图的 ID
<action
android:id="@+id/action_fragment_to_second_graph"
app:destination="@id/second_graph" />
您还可以使用扩展来合并多个图形。
看看this
实际上每个 activity 都应该有自己的导航图。
当然,您的应用程序中可以有多个导航图。每个 Activity 只能有一个导航图。我刚刚为不同的 activity 添加了两个 Nav_Graph。工作正常。这是我的导航文件夹的屏幕截图。
所以我在使用 Jetpack navigation
并且片段的数量不断增加。
我们可以在不同的导航图中分离片段,如本文档所述
然后我尝试将不同的导航图放在不同的文件中,因为这样感觉文件更有组织性和可读性,但是当我尝试导航到不同的 nav_graph 文件时出现以下错误。
nav_graph_start.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_start"
app:startDestination="@id/splashScreen"
tools:ignore="UnusedNavigation">
<fragment
android:id="@+id/splashScreen"
android:name="com.timetoface.android.splash.SplashFragment"
android:label="Login Fragment"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splash_to_login"
app:destination="@id/nav_graph_auth"
/>
<action
android:id="@+id/action_splash_to_home"
app:destination="@id/nav_graph_home"
/>
</fragment>
</navigation>
nav_graph_auth.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_auth"
app:startDestination="@id/emailLoginScreen"
tools:ignore="UnusedNavigation">
................................
</navigation>
nav_graph_home.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_home"
app:startDestination="@id/emailLoginScreen"
tools:ignore="UnusedNavigation">
................................
</navigation>
navigation destination com.app.android:id/nav_graph_home referenced from action com.app.android:id/action_splash_to_home is unknown to this NavController
所以,
多个导航图文件还不支持吗?
我是否遗漏了一些我应该改变的东西?
首先你可以使用include。看看this
示例:first_graph.xml
<include app:graph="@navigation/second_graph" />
然后将操作设置为包含图的 ID
<action
android:id="@+id/action_fragment_to_second_graph"
app:destination="@id/second_graph" />
您还可以使用扩展来合并多个图形。
看看this
实际上每个 activity 都应该有自己的导航图。
当然,您的应用程序中可以有多个导航图。每个 Activity 只能有一个导航图。我刚刚为不同的 activity 添加了两个 Nav_Graph。工作正常。这是我的导航文件夹的屏幕截图。