简体   繁体   English

使用 arguments 的多模块 android 架构中的导航

[英]Navigation in multi-module android architecture with arguments

Since the navigation files are in app module, feature modules won't have access to the Args file.由于导航文件位于 app 模块中,因此功能模块无法访问 Args 文件。 Is there any workaround for that?有什么解决方法吗?

example: if I have a fragment which accepts email and password like so例如:如果我有一个接受 email 和密码的片段,就像这样

<fragment
        android:id="@+id/navigation_sample"
        android:name="SampleFragment">
        <argument
            android:name="email"
            app:argType="string" />
        <argument
            android:name="password"
            app:argType="string" />
    </fragment>

I would have a SampleFragmentArgs generated which could be used to navigate我会生成一个可用于导航的 SampleFragmentArgs

findnavController.navigate(R.id.navigation_sample, SampleFragmentArgs(email, password).toBundle)

But I can use this only from app module not in a feature module但是我只能从应用程序模块而不是功能模块中使用它

It depends on what you call a "feature module", however I'm assuming that should be android library.这取决于您所说的“功能模块”,但是我假设应该是 android 库。

This is the setup that works:这是有效的设置:

  1. Say your android library is called "mylibrary"假设您的 android 库称为“mylibrary”
  2. Your "app" module naturally should have the above as dependency您的“应用程序”模块自然应该具有以上作为依赖项
  3. Your "mylibrary" should have apply plugin: "androidx.navigation.safeargs.kotlin" and corresponding setting in classpath of library top level build.gradle您的“mylibrary”应该有apply plugin: "androidx.navigation.safeargs.kotlin"和库顶级 build.gradle 的类路径中的相应设置
  4. Let's declare navigation graph inside library (say, mylibrary_graph.xml with id myLibraryGraph), just like you'd do in "app" module - and let's assume it has your "SampleFragment" as starting point, with all those arguments listed.让我们在库中声明导航图(例如,mylibrary_graph.xml,id 为 myLibraryGraph),就像您在“app”模块中所做的那样 - 假设它以您的“SampleFragment”为起点,列出所有这些 arguments。
  5. Let's say your "app" module has some navigation graph and let's include "mylibrary_graph.xml" as nested there ( <include app:graph="@navigation/mylibrary_graph" /> )假设您的“app”模块有一些导航图,让我们将“mylibrary_graph.xml”嵌套在那里( <include app:graph="@navigation/mylibrary_graph" />
  6. Let's say your "app" module nav graph in question has some fragment that needs to jump to your "SampleFragment" inside your android library.假设您的“应用程序”模块导航图有一些片段需要跳转到 android 库中的“SampleFragment”。 We'll declare it like this (pseudo code):我们将像这样声明它(伪代码):
    <fragment
        android:id="@+id/HelloWorldFragment"
        android:name="..."
        android:label="..."
        tools:layout="...">

        <action
            android:id="@+id/jumpToSampleFragment"
            app:destination="@+id/myLibraryGraph">

          <argument
              android:name="email"
              app:argType="string" />
          <argument
              android:name="password"
              app:argType="string" />
        </action>
    </fragment>
  1. Finally, you can then do this inside your HelloWorldFragment: findNavController().navigate(HelloWorldFragmentDirections.jumpToSampleFragment("oh@acme.com", "nimda"))最后,您可以在 HelloWorldFragment 中执行此操作: findNavController().navigate(HelloWorldFragmentDirections.jumpToSampleFragment("oh@acme.com", "nimda"))

As one can see it works but there is somewhat minor inconvenience of declaring arguments in 2 places.正如人们所看到的,它可以工作,但是在 2 个地方声明 arguments 有点不便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM