Pass Data with Safe Args

You may have noticed that the SingleTeamFragment class isn’t happy right now because it doesn’t know about SingleTeamFragmentArgs or that navArgs function. Also note that the problematic line looks suspiciously close to how we grab a ViewModel class.

In Android, the “traditional” way to send data between locations is via a Bundle, where you add key/value pairs and either send them directly into a class or as part of an Intent. Instead of doing that, we’re going to take advantage of the Navigation Safe Args plugin.

This plugin allows us to send typed values between destinations rather than String values we have to convert into something else manually. We then can reference a NavArgs class that’s generated for us to ...

Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.