【问题标题】:How to send a data class object from one module to another module?如何将数据类对象从一个模块发送到另一个模块?
【发布时间】:2021-05-29 13:44:01
【问题描述】:

我正在使用 Android 导航组件和单个 Activity 架构。我想将一个数据类对象从模块 A 发送到模块 B。

我的 feed 模块在 build.gradle 中导入 store_article 模块。

从我的 feed 模块到我的 store_article 模块的重定向有效。

我的 feed_nav_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>

我现在如何从我的 store_article 模块访问数据类对象?

我不能这样做val args: StoredArticleScreenFragmentArgs by navArgs(),因为我必须导入 feed 模块,这会导致循环依赖错误。

class StoredArticleScreenFragment : Fragment() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

}

【问题讨论】:

    标签: android kotlin android-architecture-navigation multi-module data-class


    【解决方案1】:

    我敢打赌这是因为Article-model(StoredArticleScreenFragment 中的app:argType="news.feed.model.Article")。你应该在store_article-module 中使用一个额外的模型来避免循环依赖。所以storedArticleScreenFragment 应该有自己的模型来告诉调用者片段需要什么参数。

    【讨论】:

    • 不,这不是问题所在。不需要额外的模型。文章模型位于核心模块中。模块 feed 和 stored_article 模块都在它们的 build.gradle 中实现了核心模块。我必须使用与来自 stored_article 模块的 stored_articles_nav_graph.xml 中的参数完全相同的片段声明。
    【解决方案2】:

    我发现解决方案是使用与 stored_articles 模块中的 stored_articles_nav_graph.xml 中的参数完全相同的片段声明。

    stored_article 模块中的 stored_articles_nav_graph.xml 如下所示:

    <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>
    

    现在,我可以调用 navArgs()feed 模块获取发送文章对象。

    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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2019-04-14
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      相关资源
      最近更新 更多