androidstringandroidxandroid-navigationandroid-safe-args

How to get a default string value from resources in SafeArgs?


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):

enter image description here

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?


Solution

  • 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.

    enter image description here