Android是否可以有多个nav_graph文件? [英] Android Is it possible to have multiple nav_graph files?

查看:96
本文介绍了Android是否可以有多个nav_graph文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用的是 Jetpack导航,碎片的数量一直在增长.

我们可以按照本文档中的说明在不同的导航图中分隔片段

jetpack导航图文档

然后,我尝试将不同的导航图放置在不同的文件中,因为这会使文件看起来更有条理和可读性,但是当我尝试导航到不同的nav_graph文件时,出现以下错误.

nav_graph_start.xml

 <导航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"工具:ignore ="UnusedNavigation"><片段android:id ="@ + id/splashScreen"android:name ="com.timetoface.android.splash.SplashFragment"android:label =登录片段"工具:layout ="@ layout/fragment_splash"><动作android:id ="@ + id/action_splash_to_login"app:destination ="@ id/nav_graph_auth"/><动作android:id ="@ + id/action_splash_to_home"app:destination ="@ id/nav_graph_home"/></fragment></导航> 

nav_graph_auth.xml

 <导航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"工具:ignore ="UnusedNavigation">................................</导航> 

nav_graph_home.xml

 <导航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"工具:ignore ="UnusedNavigation">................................</导航> 

导航目标com.app.android:id/nav_graph_home从动作com.app.android:id/action_splash_to_home引用NavController未知

所以

是否尚不支持m 个通用导航图文件?

我错过了我应该改变的东西吗?

解决方案

首先可以使用include.看看

示例:first_graph.xml

 < include app:graph ="@ navigation/second_graph"/> 

然后将操作设置为包含图的ID

 < actionandroid:id ="@ + id/action_fragment_to_second_graph"app:destination ="@ id/second_graph"/> 

此外,您还可以使用扩展名来合并多个图形.

看看

实际上,每个活动都应具有自己的导航图.

So I was using Jetpack navigation and the number of fragments kept growing up.

We can separate fragments in different navigation graph as described in this document

jetpack nav graph docs

Then I tried to put different nav graphs in different files because that felt more organized and readable file but I get the following error when I try to navigate to different nav_graph files.

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

So,

Are multiple navigation graph files not supported yet?

Am I missing something that I should change?

解决方案

First of all you can use include. Take a look this

example: first_graph.xml

<include app:graph="@navigation/second_graph" />

then set action to included graph's id

 <action
        android:id="@+id/action_fragment_to_second_graph"
        app:destination="@id/second_graph" />

Also you can use extension to use multiple graphs merged.

Take a look to this

Actually every activity should have it's own nav graph.

这篇关于Android是否可以有多个nav_graph文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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