Fixed
Status Update
Comments
il...@google.com <il...@google.com>
il...@google.com <il...@google.com> #2
This is an issue when using inferred types (i.e., not specifying an app:argType) and android:defaultValue="@null".
You can work around this issue by specifying app:argType="string" on your <argument>:
<action
android:id="@+id/createModel"
app:destination="@id/editFragment" >
<argument
android:name="modelId"
app:argType="string"
android:defaultValue="@null" />
</action>
As an alternative, you can instead add your android:defaultValue="@null" to your android:id="@+id/editFragment"'s argument and leave out the <argument> entirely on your action, but that does have a slightly different semantic meaning when it comes to EditFragmentArgs' parsing.
You can work around this issue by specifying app:argType="string" on your <argument>:
<action
android:id="@+id/createModel"
app:destination="@id/editFragment" >
<argument
android:name="modelId"
app:argType="string"
android:defaultValue="@null" />
</action>
As an alternative, you can instead add your android:defaultValue="@null" to your android:id="@+id/editFragment"'s argument and leave out the <argument> entirely on your action, but that does have a slightly different semantic meaning when it comes to EditFragmentArgs' parsing.
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-master-dev
commit f3dff7e6da2365602626d0d24b68c340db4c9c7c
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Mon Apr 01 22:36:01 2019
Use NullValue when inferring String with "@null" as default value.
Bug: 129629192
Test: ./gradlew navigation:navigation-safe-args-gradle-plugin:test
Change-Id: Ide6ecceac6de75f207b1ffaa080c5f5b7f5c2b66
M navigation/safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/NavParser.kt
M navigation/safe-args-generator/src/tests/kotlin/androidx/navigation/safe/args/generator/NavParserTest.kt
M navigation/safe-args-generator/src/tests/test-data/naive_test.xml
https://android-review.googlesource.com/937354
https://goto.google.com/android-sha1/f3dff7e6da2365602626d0d24b68c340db4c9c7c
Branch: androidx-master-dev
commit f3dff7e6da2365602626d0d24b68c340db4c9c7c
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Mon Apr 01 22:36:01 2019
Use NullValue when inferring String with "@null" as default value.
Bug: 129629192
Test: ./gradlew navigation:navigation-safe-args-gradle-plugin:test
Change-Id: Ide6ecceac6de75f207b1ffaa080c5f5b7f5c2b66
M navigation/safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/NavParser.kt
M navigation/safe-args-generator/src/tests/kotlin/androidx/navigation/safe/args/generator/NavParserTest.kt
M navigation/safe-args-generator/src/tests/test-data/naive_test.xml
mm...@commonsware.com <mm...@commonsware.com> #4
Regarding the workarounds for #2, the first one requires app:nullable="true" both on the destination and the action. IOW, I wind up with an action like:
<action
android:id="@+id/createModel"
app:destination="@id/editFragment">
<argument
android:name="modelId"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
</action>
That's fine -- I only mention it because the code snippet in #2 did not have it.
The second approach (put the default value on the destination's argument) also works, and in my case it's a better solution anyway, so I'll go with that.
Many thanks!
<action
android:id="@+id/createModel"
app:destination="@id/editFragment">
<argument
android:name="modelId"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
</action>
That's fine -- I only mention it because the code snippet in #2 did not have it.
The second approach (put the default value on the destination's argument) also works, and in my case it's a better solution anyway, so I'll go with that.
Many thanks!
il...@google.com <il...@google.com> #5
You're correct - if you're using android:defaultValue="@null", you'd need app:nullable="true".
This has been fixed internally and will be available in Navigation 2.1.0-alpha03 (it missed the alpha02 cutoff).
This has been fixed internally and will be available in Navigation 2.1.0-alpha03 (it missed the alpha02 cutoff).
Description
Version used: 2.0.0
Devices/Android versions reproduced on: n/a
Android Studio 3.3.2
Build #AI-182.5107.16.33.5314842, built on February 15, 2019
JRE: 1.8.0_152-release-1248-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-46-generic
com.android.tools.build:gradle:3.3.2
gradle-4.10.3-all
I have a destination with a nullable String argument:
<fragment
android:id="@+id/editFragment"
android:name="com.commonsware.todo.ui.edit.EditFragment"
android:label="@string/app_name" >
<argument
android:name="modelId"
app:argType="string"
app:nullable="true" />
</fragment>
I have an action that attempts to declare null as the default value:
<action
android:id="@+id/createModel"
app:destination="@id/editFragment" >
<argument
android:name="modelId"
android:defaultValue="@null" />
</action>
The generated SafeArgs code treats @null as a String:
private data class CreateModel(val modelId: String = "@null") : NavDirections {
override fun getActionId(): Int = R.id.createModel
override fun getArguments(): Bundle {
val result = Bundle()
result.putString("modelId", this.modelId)
return result
}
}
fun createModel(modelId: String = "@null"): NavDirections = CreateModel(modelId)
IOW, I am seeing similar symptoms to
If I remove the default value, the SafeArgs-generated code goes back to supporting null, just without a default null:
private data class CreateModel(val modelId: String?) : NavDirections {
override fun getActionId(): Int = R.id.createModel
override fun getArguments(): Bundle {
val result = Bundle()
result.putString("modelId", this.modelId)
return result
}
}
fun createModel(modelId: String?): NavDirections = CreateModel(modelId)
Let me know if you need additional information -- thanks!