1

I have a case here that I have to open a Fragment from an Activity and I did it sometimes but the issue here that I wanna open the Fragment that has an argument with it.

<fragment
    android:id="@+id/productDetailsFragment"
    android:name="com.a.ry.ui.product.ProductDetailsFragment"
    android:label="fragment_product_details2"
    tools:layout="@layout/fragment_product_details" >
    <argument
        android:name="productId"
        app:argType="integer" />
</fragment>

What I know is that I can open a Fragment (From Activity) and pass its Argument like this:

val bundle = Bundle()
bundle.putInt("productId", productIdValue)
navController.navigate(R.id.productDetailsFragment, bundle)

the problem is here "productId" what if another developer change that name from navigation.xml file so it's not the best practice at all

I want to take the same reference of that argument name.

Or if there is another solution for that?

Amr263
  • 143
  • 2
  • 8
  • 1
    SafeArgs plugin generates a special Direction class for each action. You should use this class. – sdex Feb 26 '21 at 09:31

1 Answers1

0

You should define it in string.xml and use in XML and code.

android:name="@string/VAR"

 getString (R.string.VAR);
Gaurav
  • 3,615
  • 2
  • 27
  • 50