I am migrating from XML to Compose Navigation. Now I'm trying to pass an array of enums as an argument. My enum class looks like this,
@Parcelize
enum class MyEnum : Parcelable {
Option1,
Option2
}
This is how I define an argument in an XML file,
<argument
android:name="enumArg"
app:argType="com.test.MyEnum[]"
app:nullable="false" />
And I was passing it by calling a function like this
navController.navigate(MyFragmentDirections.actionFragmentAToFragmentB(enumList.toTypedArray())
Now, in the Jetpack Compose way, MyEnum
class looks still the same and I defined my argument in the composable destination like this:
navArgument(name = "enumArg") {
type = NavType.ParcelableArrayType(MyEnum::class.java)
nullable = false
}
but the navController.navigate()
function throws an exception:
java.lang.IllegalArgumentException: Wrong argument type for 'enumArg' in argument bundle. [Lcom.test.MyEnum; expected.
I'm sure that I'm passing Array<MyEnum>
. How should I pass these arguments? I can't find any example of ParcelableArrayType usage.