12

I am using safe-args to pass arguments from one fragment to the other. Android studio intermittently generates the fragmentArgs class with all the arguments.

What I have tried, and does work, is altering the nav graph file, making the project, then undo those changes, and finally, make the project again.

Dependencies Used:

Project module:

ext.nav_version = '2.2.0-beta01'

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

App module:

apply plugin: "androidx.navigation.safeargs.kotlin"

    <fragment
            android:id="@+id/someFragment"
            android:name="package_name.SomeFragment"
            android:label="@string/some_fragment">
        <argument
                android:name="source"
                android:defaultValue="-1"
                app:argType="integer" />
        <argument
                android:name="nationalId"
                android:defaultValue="-1"
                app:argType="integer" />
        <argument
                android:name="hudumaNumber"
                android:defaultValue="-1"
                app:argType="string" />
        <argument
                android:name="middleName"
                android:defaultValue="None"
                app:argType="string" />

    </fragment>

I expect that all the arguments will be found in the generated class, but that is not the case.

wamae
  • 660
  • 9
  • 21

3 Answers3

12

Try switching to:

apply plugin: "androidx.navigation.safeargs"

instead of:

apply plugin: "androidx.navigation.safeargs.kotlin"

This will generate Java code instead of Kotlin code inside your app\build\generated folder. This doesn't effect your own code and you can continue to use Kotlin or Java for your Fragments.

Stephen Mullen
  • 216
  • 4
  • 7
  • Switching to `androidx.navigation.safeargs` indeed helped. I wonder why is this happening tho – qki Jan 27 '21 at 19:22
  • NB: Java functions cannot have named parameters eg `foo(bar = 1)` so if you use those, you will get an error when making this change – MSpeed Dec 10 '21 at 16:03
1

You just need to rebuild your project,

  1. Delete any lines that cause error for now
  2. Rebuild your project
  3. Add those lines again
Mahmoud Hendi
  • 139
  • 11
1

My problem was that I had deleted the name attribute in nav_graph.xml

 <fragment
        android:id="@+id/emailVerifyFragment"
        android:name="com.package.name.EmailVerifyFragment" <<<--- this is necessary!
        tools:layout="@layout/fragment_email_address_verify">
MSpeed
  • 8,153
  • 7
  • 49
  • 61