具有架构组件的多模块导航 [英] Multi-module Navigation with Architecture Components

查看:93
本文介绍了具有架构组件的多模块导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此我在当前应用中的模块具有这种结构.

So I have this structure for my modules in my current app.

我还没有找到有关多模块导航的任何官方文档,但是我找到了这个

I haven't found any official documentation on multi-module navigation yet but I found this article regarding this so here's how my gradle files are:

功能1-详细信息

...
implementation project(":base")
implementation project(":feature-2-detail")
...

功能2-详细信息

...
implementation project(":base")
implementation project(":feature-1-detail")
...

功能3-详细信息

...
implementation project(":base")
implementation project(":feature-1-detail")
...

这是我的导航图:

功能1-详细信息

<navigation ...
    android:id="@+id/graph_feature_1_id">
    <include app:graph="@navigation/graph_feature_2" />
    <fragment ...
        android:id="@+id/nav_feature_1">
        <action ...
            app:destination="@+id/graph_feature_2_id" />

    </fragment>
</navigation>

功能2-详细信息

<navigation ...
    android:id="@+id/graph_feature_2_id">
    <include app:graph="@navigation/graph_feature_1" />
    <fragment ...
        android:id="@+id/nav_feature_2">
        <action ...
            app:destination="@+id/graph_feature_1_id" />

    </fragment>
</navigation>

功能3-详细信息

<navigation ...
    android:id="@+id/graph_feature_3_id">
    <include app:graph="@navigation/graph_feature_1" />
    <fragment ...
        android:id="@+id/nav_feature_3">
        <action ...
            app:destination="@+id/graph_feature_1_id" />

    </fragment>
</navigation>

因此,所有设置都适用于这种设置,但是这里的问题是要将模块连接到另一个模块,我们必须将另一个功能添加为对当前功能的依赖.就像我的情况一样,功能1-详细信息可以转到功能2-详细信息,反之亦然,这样做使我有了循环依赖.

So everything works with this kind of setup but the problem here is that to connect the module to another module, we have to add the other feature as a dependency to the current feature. Like in my case, Feature 1 - Detail can go to Feature 2 - Detail and vice versa and doing this gives me a circular dependency in gradle.

还有另一种方法可以进行多模块导航吗?我尝试使用深层链接,但无济于事.

Is there another way to do multi-module navigation? I've tried using deep links but to no avail.

任何帮助将不胜感激!谢谢!

Any help would be appreciated! Thanks!

推荐答案

这已经一年了,但是库现在可以支持这个确切的用例!从 2.1.0-alpha03 开始,我们可以导航通过深层链接URI.

This is already a year-long but the library now can support this exact use-case! As of 2.1.0-alpha03, we can navigation through deep link URIs.

我们无需将功能作为实现细节彼此添加,而是可以使它们之间不知道并使用深度链接导航.

Instead of adding the features as implementation details to each other, we can leave them unaware between themselves and use deep link navigation.

功能1-详细信息-build.gradle

dependencies {
    implementation project(':base')
}

功能2-详细信息相同.无需知道其他模块.

Same with Feature 2 - Detail. No need for it to know the other modules.

要进行模块间导航,我们首先必须定义一个深度链接,以通过 deepLink 标签在该目的地中进行导航.

To have inter-module navigation, we have to first define the deep link for navigating through that destination via a deepLink tag.

功能1-详细信息-导航图

<navigation ...
    android:id="@+id/graph_feature_1_detail_id">
    <fragment ...
        android:id="@+id/nav_feature_1_detail">
        <deepLink app:uri="myApp://feature1detail"/>

    </fragment>
</navigation>

功能2-详细信息-导航图

<navigation ...
    android:id="@+id/graph_feature_2_detail_id">
    <fragment ...
        android:id="@+id/nav_feature_2_detail">
        <deepLink app:uri="myApp://feature2detail"/>

    </fragment>
</navigation>

现在我们已经设置了URI的深层链接,我们可以直接在 NavController

Now that we have deep links with URIs set, we can directly use this in a NavController

因此,在功能1-详细信息的片段中,也许是单击按钮?您必须执行导航的任何地方

So in the fragment in Feature 1 - Detail, maybe on a button click? Anywhere where you have to perform navigation

class Feature1DetailFragment {
   fun onViewCreated(...) {
       ...
       view.setOnClickListener {
           val uri = Uri.parse("myApp://feature2detail")
           findNavController().navigate(uri)
       }
   }
}

功能2-详细信息中,

class Feature2DetailFragment {
   fun onViewCreated(...) {
       ...
       view.setOnClickListener {
           val uri = Uri.parse("myApp://feature1detail")
           findNavController().navigate(uri)
       }
   }
}

瞧!模块间导航.

在撰写本文时,最新的稳定版本是 2.1.0-rc01 .

At the time of writing, the latest stable release is 2.1.0-rc01.

尽管我还没有在更复杂的项目上尝试过这一点,但我喜欢这个库,并且希望看到这个库更加成熟!

Although I haven't tried this out on more complex projects, I love this library and I'm hoping to see this library mature more!

我创建了一个关于此的中篇文章.您可以看一下.干杯!

I created a Medium article about this. You can take a look at it. Cheers!

这篇关于具有架构组件的多模块导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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