How to migrate to Safe Args from an older API?

To migrate from an older API to Safe Args in your Android project, you need to follow several steps that ensure a smooth transition. Safe Args is a Gradle plugin that generates type-safe classes and methods for navigating between destinations in your Android applications. This process helps prevent runtime errors related to navigation arguments.

Step-by-Step Migration Process

  1. Update your dependencies: Ensure you have included the Safe Args Gradle plugin in your build.gradle file.
  2. Define your navigation graph: Create or update your navigation XML file to define your fragments and their arguments.
  3. Use Safe Args: Replace the existing navigation method calls with the generated Safe Args classes.
  4. Test your changes: Run your application and verify that the navigation and argument passing work as expected.

Example of Migrating to Safe Args

// Old API Navigation NavHostFragment.findNavController(this) .navigate(R.id.action_fragmentA_to_fragmentB, bundleOf("arg1" to value)); // New Safe Args Navigation val action = FragmentADirections.actionFragmentAToFragmentB(value) findNavController().navigate(action)

keywords: Android Safe Args migration type-safe navigation Gradle plugin navigation graph