Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 974
AlexCarlayl Avatar asked Jul 30 '19 15:07

AlexCarlayl


People also ask

What is the primary strength of SafeArgs?

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.

Why do we use safe args instead of bundle approach?

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.


1 Answers

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

  • https://developer.android.com/guide/navigation/navigation-pass-data#supported_argument_types
  • https://stackoverflow.com/a/55701078/4694013
like image 142
Anoop M Maddasseri Avatar answered Sep 18 '22 17:09

Anoop M Maddasseri