I'm just learning a Android NavigationUI, and try set a toolbar title using string default value from Safe Args. But have some problem about it.
'String resources' file:
<string name="title_add_item">Add new items</string>
Navigation graph file. Set label as Title: {title}
argument.
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/addNewItemFragment"
android:name="com.myapplication.AddNewItemFragment"
android:label="Title: {title}"
tools:layout="@layout/fragment_add_new_item" >
<argument
android:name="title"
app:argType="string"
android:defaultValue="@string/title_add_item" />
</fragment>
<fragment
android:id="@+id/mainFragment"
android:name="com.myapplication.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_to_add_items_fragment"
app:destination="@id/addNewItemFragment" />
</fragment>
If {app:argType="string"}
I got an error :
Caused by: org.xmlpull.v1.XmlPullParserException: unsupported value 'Add new items' for string. You must use a "reference" type to reference other resources.
If {app:argType="reference"}
app works, but i have a number in title (i think it's a resource id):
Of course I can assign the value of the toolbar title in the code by getting it from the arguments. But is it possible to change this code so that the title filled in correctly?
SafeArgs is a gradle plugin that allows you to Pass data to destination UI components. It generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data because it ensures type-safety.
Safe Args is strongly recommended for navigating and passing data, because it ensures type-safety. In some cases, for example if you are not using Gradle, you can't use the Safe Args plugin. In these cases, you can use Bundles to directly pass data. You must also apply one of two available plugins.
References to resources are supported only in reference types. Using a resource reference in any other type results in an exception.
Feel free to star the existing issue: https://issuetracker.google.com/issues/167959935. For the time being, u can hardcode the string value with app:argType="string"
or try setting programmatically until it gets resolved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With