Android 如何将数据类对象从一个模块发送到另一个模块?
我使用的是Android导航组件和单一活动架构。我想将数据类对象从模块a发送到模块B My提要模块在build.gradle存储文章模块中导入Android 如何将数据类对象从一个模块发送到另一个模块?,android,kotlin,android-architecture-navigation,multi-module,data-class,Android,Kotlin,Android Architecture Navigation,Multi Module,Data Class,我使用的是Android导航组件和单一活动架构。我想将数据类对象从模块a发送到模块B My提要模块在build.gradle存储文章模块中导入 class StoredArticleScreenFragment : Fragment() { private val args: StoredArticleScreenFragmentArgs by navArgs() override fun onViewCreated(view: View, savedInstanceStat
class StoredArticleScreenFragment : Fragment() {
private val args: StoredArticleScreenFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val article: Article = args.article
}
}
从我的提要模块重定向到我的商店文章模块起作用
我的feed\u nav\u graph.xml看起来像这样:
<fragment
android:id="@+id/newsDetailFragment"
android:name="news.feed.view.ui.NewsDetailFragment"
tools:layout="@layout/fragment_news_detail">
<argument
android:name="article"
app:argType="feed.model.Article" />
<action
android:id="@+id/action_newsDetailFragment_to_storedArticleScreenFragment"
app:destination="@id/storedArticleScreenFragment" />
</fragment>
<fragment
android:id="@+id/storedArticleScreenFragment"
android:name="news.stored_article.view.ui.StoredArticleScreenFragment"
android:label="Stored Articles"
tools:layout="@layout/fragment_stored_article_screen">
<argument
android:name="article"
app:argType=".core.Article" />
</fragment>
<fragment
android:id="@+id/storedArticleScreenFragment"
android:name="stored_article.view.ui.StoredArticleScreenFragment"
android:label="Stored Articles"
tools:layout="@layout/fragment_stored_article_screen">
<argument
android:name="article"
app:argType=".core.Article" />
</fragment>
我打赌这是因为
文章
-model(应用程序:argType=“news.feed.model.Article”
中的屏幕片段
)。您应该在store\u article
-模块中使用额外的模型,以避免循环依赖。因此storedArticleScreenFragment
应该有自己的模型来告诉调用方片段需要什么参数。我发现解决方案是使用与存储文章导航图.xml中的参数完全相同的片段声明
class StoredArticleScreenFragment : Fragment() {
private val args: StoredArticleScreenFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val article: Article = args.article
}
}
来自存储文章的存储文章\u导航图.xml模块如下所示:
<fragment
android:id="@+id/newsDetailFragment"
android:name="news.feed.view.ui.NewsDetailFragment"
tools:layout="@layout/fragment_news_detail">
<argument
android:name="article"
app:argType="feed.model.Article" />
<action
android:id="@+id/action_newsDetailFragment_to_storedArticleScreenFragment"
app:destination="@id/storedArticleScreenFragment" />
</fragment>
<fragment
android:id="@+id/storedArticleScreenFragment"
android:name="news.stored_article.view.ui.StoredArticleScreenFragment"
android:label="Stored Articles"
tools:layout="@layout/fragment_stored_article_screen">
<argument
android:name="article"
app:argType=".core.Article" />
</fragment>
<fragment
android:id="@+id/storedArticleScreenFragment"
android:name="stored_article.view.ui.StoredArticleScreenFragment"
android:label="Stored Articles"
tools:layout="@layout/fragment_stored_article_screen">
<argument
android:name="article"
app:argType=".core.Article" />
</fragment>
不,这不是问题所在。不需要额外的型号。文章模型位于核心模块中。这两个模块都在其build.gradle核心模块中实现了feed和storaged_article模块。我必须使用与存储文章模块中存储文章中的参数完全相同的片段声明。