导航组件重复NavArgs出现问题 [英] Issue with Navigation Component Duplicate NavArgs

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

问题描述

我有一个片段:

class SomeFragment {
    private val args by navArgs<SomeFragmentArgs>()
}

此片段用于两个导航图:

this fragment is used in two navigation graph:

first_nav.xml

....
<fragment
    android:id="@+id/initialFragment"
    android:name="com.example.InitialFragment"
    android:label="Initial Fragment">
    <action
        android:id="@+id/action_to_some_fragment"
        app:destination="@id/someFragment" />
</fragment>
<fragment
    android:id="@+id/someFragment"
    android:name="com.example.SomeFragment"
    android:label="Some Label">
    <argument
        android:name="someType"
        app:argType="com.example.someType"/>
</fragment>
....

second_nav.xml

....
<fragment
    android:id="@+id/initialFragment2"
    android:name="com.example.InitialFragment2"
    android:label="Initial Fragment">
    <action
        android:id="@+id/action_to_some_fragment"
        app:destination="@id/someFragment" />
</fragment>
<fragment
    android:id="@+id/someFragment"
    android:name="com.example.SomeFragment"
    android:label="Some Label">
    <argument
        android:name="someType"
        app:argType="com.example.someType"/>
</fragment>
....

但是当我为R8版本构建项目时抛出:

But when I build the project for release R8 throws:

R8:程序类型已经存在:com.example.SomeFragmentArgs $ Companion

有人可以帮我吗?

推荐答案

我也面临这个问题.我们正在使用多个模块化开发,并且方向类是在不同的模块中生成的.这样到处都是几个FragmentDirections,导致R8程序类型已经存在,因为它们都得到了伴随对象.

I am facing this issue too. We are using multiple modular developing and the direction class are generated in different modules. So that there are several FragmentDirections in everywhere and cause R8 Program type already present since they all got companion object.

有两种解决方案/解决方法.

There are two solutions/workarounds.

  1. 将此片段隔离为一个独立的navigation.xml

根据您的情况,您可以编写some_fragment_nav.xml

In your case you can write a some_fragment_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@id/someFragment"
android:id="@+id/some_fragment_nav">

<fragment
    android:id="@+id/someFragment"
    android:name="com.example.SomeFragment"
    android:label="lable">

    <action
        android:id="@+id/what_ever_action_you_want"
        app:destination="@id/share_this_id_to_ids" />


    <argument
        android:name="someType"
        app:argType="com.example.someType"/>

</fragment>

</navigation>

并使用

<include app:graph="@navigation/some_fragment_nav"/>

在两个导航图中都为

.那可行.如果您正在使用多模块显影.您需要通过将ID写入ids.xml来共享操作ID.

in both of your navigation graphs. That will work. If you are using multi-modular developing. You need to share action id by writing your id into ids.xml.

  1. 将SomeFragment复制并过去到另一个命名中,例如SomeFragment1.kt SomeFragment2.kt.而且也不会存在任何R8程序类型.此外,您可以使SomeFragment作为开放类,而SomeFragment1通过空实现扩展SomeFragment.

我的个人前景.2比1好,因为它们在我看来都是解决方法.直到喷气机更改生成的FragmentDirection类的规则.

My personal prospects.. 2 is better than 1 since they all look like workarounds to me. Until jetbrains change the rules of generated FragmentDirection class.

这篇关于导航组件重复NavArgs出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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