why navigation arguments of type Int, Long, Float ...
# android
k
why navigation arguments of type Int, Long, Float are not nullable?
b
I believe they are using primitive types under the hood which can’t be null in any case. You can check a table in here which types are supported as null: https://developer.android.com/guide/navigation/navigation-pass-data
k
Yeah, but on one end it’s said that prefer passing id as an argument and fetch that object from db over passing a
Parcelable
object while on the other end, if I want that id to be nullable I can’t do it with primitive types so possibly I need to fallback to a custom
Parcelable
. How this makes sense?
b
Yeah, I am afraid that you will need to work with those restrictions. Or take a look if you can prevent navigation to the next screen in the first place. Add some changes to the whole UX flow if it’s possible. For example, if the screen that you are trying to navigate with a nullable id is showing only data from that object, then you could prevent the user from navigating further to the screen with no data on it. Another solution for you is maybe you can replace your behavior depending on
null
with some default value, for example,
-1
in your code. As you can see from that table, default values can be used for those types.
k
Yup. looks like I need to go ahead with default values, seems pretty fragile solution though.