Navigation component, argument type and sealed class

Navigation component, argument type and sealed class

April 24, 2021
Mateusz Teteruk
If you use Navigation component you are familiar with code snippet like this:
<navigation> .... <fragment ...> <argument android:name="name" app:argType="string" /> </fragment> ....
In here we have navigation, fragment and fragment has one String argument called „name”. All supported argument types can be found in documentation. Navigation component also supports custom parcelable types.

Subclass of a sealed class

sealed class Animal { .... @Parcelize data class Cat(val name: String) : Animal(), Parcelable }
But what to do if we have sealed class and we want to specify only one subclass as argument type? For example, we have fragment which only takes Cat class as argument.
Probably you would to do it as normal:
<argument android:name="cat" app:argType="" />
but you would encounter ClassNotFoundException. So what’s wrong?


Actually, the problem is in „Animal (dot) Cat” part. You have to replace dot with $ sign.
<argument android:name="cat" app:argType="$Cat" />
Now everything should look good and work. There is no info about that in documentation so please be aware! Also if you have another nested sealed class in sealed class, use $ as separator.