Add safeArgs

 1. Add safe-args dependency to project Gradle file

SafeArgs is implemented as a Gradle plugin, which means that it is a two-step process to add. First, we need to add the navigation-save-args-gradle-plugin dependency into the project Gradle file, which shows up in the Android view labeled Project: andfun-kotlin-android-trivia. SC - Project Gradle

// Adding the safe-args dependency to the project Gradle file
dependencies {
   …
"android.arch.navigation:navigation-safe-args-gradle-plugin:$version_navigation"

   // NOTE: Do not place your application dependencies here; they belong
   // in the individual module build.gradle files
}

2. Add safeargs plugin to app Gradle file

At the top of your app Gradle file, after all of the other plugins, add the apply plugin statement with the androidx navigation safeargs plugin.

// Adding the apply plugin statement for safeargs
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'

3. Switch the GameFragment to use generated NavDirections when navigating to the GameOver and GameWon fragments.

Next, go to the code for the Game fragment. First, replace the action ID for the game won state with GameFragmentDirections.actionGameFragmentToGameWonFragment().

// Using directions to navigate to the GameWonFragment
view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameWonFragment())

Next, do the same thing for the game over state.

// Using directions to navigate to the GameOverFragment
view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameOverFragment())

4. Add the numQuestions and numCorrect Integer Arguments using the navigation editor Next, go to the navigation editor and select the GameWon fragment. Click the little triangle next to arguments to expand the argument section. Add a numQuestions and a numCorrect argument, both with integer type. If you try to build the app now, you should get two compile errors.

No value passed for parameter 'numQuestions'
No value passed for parameter 'numCorrect'

5. Add the parameters to the gameFragment to gameWonFragment action

Let’s add those parameters! Click on the error link in the Build tab to go right to the correct place in GameFragment.kt.

// Adding the parameters to the Action
view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameWonFragment(numQuestions, questionIndex))

6. Fetch the args and expand into a class in onCreate within the GameWonFragment

So, now it will compile, but we haven’t yet extracted the parameters after the navigation is complete. We’ll use a Toast so we can see them get there successfully. Move to onCreate in GameWonFragment.kt.

7. Display the arguments using a Toast

We’ll use GameWonFragmentArgs to extract the args class from the Bundle, which we can then display in a Toast.

val args = GameWonFragmentArgs.fromBundle(arguments!!)
Toast.makeText(context, "NumCorrect: ${args.numCorrect}, NumQuestions: ${args.numQuestions}", Toast.LENGTH_LONG).show()

If Android Studio display an error, you may have to rebuild your project to make GameWonFragmentArgs available.

Run the app and see that the arguments got passed successfully to your GameWonFragment. You do have to win the Trivia game first though. (If you want, you can change it to a single question game by setting numQuestions to 1 in gameFragment to make things easier)

8. Replace navigation to action IDs with NavDirections in GameOverFragment, GameWonFragment, and TitleFragment

Since we're using safe arguments, let's use NavDirections everywhere. Replace navigation to an action ID in GameOverFragment, GameWonFragment, and TitleFragment.

Comments