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.
Use Safe Args to pass data with type safety. The Navigation component has a Gradle plugin called Safe Args that generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data, because it ensures type-safety.
SafeArgs is not the same kind of library module as the other parts of navigation; it's not an API per se, but rather a gradle plugin that will generate code. So I needed to pull it in as a gradle dependency and then apply the plugin to run at build time, to generate the necessary code.
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.
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">
You just need to rebuild your project,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With