In Passing data between fragments using SafeArgs I showed You how to pass data between -well- fragments.
This included all basic / built-in types like Strings and Integers.
In this part I will show you how you can pass custom objects.
Table of Contents
Peprare your class
To use a custom class for passing between fragments it has to be of type Parcelable and you must use the annotation @Parcelize
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
class VideoPlayListItem(val videoID: String, val videoTitle: String): Parcelable {
}
Gradle Plugin
Add kotlin-parcelize to your module gradle.build
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'androidx.navigation.safeargs.kotlin'
id "kotlin-parcelize"
Add argument in nav_graph.xml
<fragment
android:id="@+id/VideoPlayerFragment"
android:name="de.creatronix.levelup.fragments.VideoPlayerFragment"
android:label="@string/video_player_fragment_label"
tools:layout="@layout/fragment_video_player">
<argument
android:name="video"
app:argType="de.creatronix.levelup.VideoPlayListItem" />
</fragment>
Pass data to fragment action
val action =
VideoFragmentDirections.actionVideoFragmentToVideoPlayerFragment(
clickedItem
)
findNavController().navigate(action)






