In my root build.gradle I have navigation-safe-args-gradle-plugin
version 2.2.0-rc03
:
script{
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.2.0-rc03'
}
}
In my navigation graph xml:
<fragment
android:id="@+id/MyListFragment"
android:name="com.foo.bar.ui.mylist.MyListFragment"
android:label="My List">
<action
android:id="@+id/action_myListFragment_to_myDetailFragment"
app:destination="@id/myDetailFragment" />
<!--Here I pass parcelable type as argument-->
<argument
android:name="Student"
app:argType="com.foo.core.model.Student"/>
</fragment>
Student
is a normal class.
package com.foo.core.model
@Parcelize
data class Student(val studentNumber: Int): Parcelable
In my fragment I do:
val student:Student=Student(123)
findNavController().navigate(actionMyListFragmentToMyDetailFragment(student))
When I build my project, I get compiler error:
Type mismatch: inferred type is Student but String was expected
Why is that?
put your argument
in MyDetailsFragment not MyListFragment so
MyListFragment
<fragment
android:id="@+id/MyListFragment"
android:name="com.foo.bar.ui.mylist.MyListFragment"
android:label="My List">
<action
android:id="@+id/action_myListFragment_to_myDetailFragment"
app:destination="@id/MyDetailsFragment" />
</fragment>
MyDetailsFragment
<fragment
android:id="@+id/MyDetailsFragment"
android:name="com.foo.bar.ui.mylist.MyDetailsFragment"
android:label="My Details">
<argument
android:name="Student"
app:argType="com.foo.core.model.Student"/>
</fragment>