且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

导航组件重复NavArgs出现问题

更新时间:2023-12-05 11:37:10

我也面临这个问题.我们正在使用多个模块化开发,并且方向类是在不同的模块中生成的.这样到处都是几个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.