Navigation argument Double error : Can't escape identifier `0.0` because it contains illegal characters

Viewed 954

Error when setting defaultValue for Double in Navigation graph argument.

 <argument
            android:name="lat"
            app:argType="java.lang.Double"
            android:defaultValue="0.0"
            />
        <argument
            android:name="lon"
            app:argType="kotlin.Double"
            android:defaultValue="0.0"
            />

I hae tried both java.lang.Double and kotlin.Double with safe args plugin.

How can I pass a Double argument with default value?

2 Answers

You can create a Serializable class and store lat, long in that and pass an object of that class as type "custom serializable" in Navigation Arguments.

Here is a sample,

Kotlin

data class LatLong(
    var lat: Double = 0.0,
    var long: Double = 0.0
) : Serializable

Navigation Graph:

<argument
        android:name="LatLong"
        app:argType="com.example.models.LatLong" />

Safe Args currently does not support decimal type. If you don't need high precision but want to send through a decimal value (you can find differences between float and decimal here), you should just use a float. You can find supported argument types here.

If you really need high precision, you will need to get some workarounds like sending it through as a string or breaking the whole number and the decimal part into two integer values (so for 12.43, you will send in 12 as one variable and 43 as another).

Related