[英]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:这是有效的设置:
apply plugin: "androidx.navigation.safeargs.kotlin"
and corresponding setting in classpath of library top level build.gradleapply plugin: "androidx.navigation.safeargs.kotlin"
和库顶级 build.gradle 的类路径中的相应设置<include app:graph="@navigation/mylibrary_graph" />
)<include app:graph="@navigation/mylibrary_graph" />
) <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>
findNavController().navigate(HelloWorldFragmentDirections.jumpToSampleFragment("oh@acme.com", "nimda"))
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.