I use Navigation Component's Safe Args to pass data between destination
build.gradle
apply plugin: "androidx.navigation.safeargs.kotlin"
MyType.kt
@Keep
@Parcelize
class MyType(
val type: String,
val name: String
) : Parcelable
nav_graph.xml
<action android:id="@+id/startMyFragment"
app:destination="@+id/myFragment">
<argument
android:name="myArg"
app:argType="com.myapp.MyType"
app:nullable="false" />
</action>
Above navigation graph creates lot of warnings during compilation. Could someone suggest how to avoid those warnings
where K,V are type-variables:
K extends Object declared in class HashMap
V extends Object declared in class HashMap
/builds/my-app/app/build/generated/source/navigation-args/debug/com/myapp/MyType.java:19: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type HashMap
this.arguments.put("myArg", myArg);
Thanks in advance
In my Navgraph, the argument ist not within the action, here each Fragment sends the Args to the other:
additionally to the safeargs library, ich have also these:
def nav_version = '2.3.0'
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
navgraph:
<fragment
android:id="@+id/fragmentX"
android:name="package.FragmentX"
android:label="Fragment 1">
<argument
android:name="userinput"
app:argType="package.dataclasses.UserParcel"
app:nullable="true"
android:defaultValue="@null" />
<action
android:id="@+id/action_fragmentX_to_fragmentY"
app:destination="@id/fragmentY"
/>
</fragment>
<fragment
android:id="@+id/fragmentY"
android:name="package.FragmentY"
android:label="Fragment 2"
>
<argument
android:name="useripnut"
app:argType="package.dataclasses.UserParcel"
app:nullable="true"
android:defaultValue="@null" />
<action
android:id="@+id/action_fragmentY_to_fragmentX"
app:destination="@id/fragmentX" />
</fragment>
How to Get Data in Fragment X:
val args = FragmentXArgs.fromBundle(requireArguments())
How to Pass Data to Fragment Y:
val navController = activity?.findNavController(R.id.navHost)
navController?.navigate(FragmentXDirections.actionFragmentXToFragmentY(userParcel))