Use Saved Preferences

In addition to all the UI work that’s taken care of for us with the PreferenceFragmentCompat class, value changes are also saved and updated automatically. To retrieve those values, we need to get a SharedPreferences instance from the PreferenceManager class. Inside GameViewModel we’re going to do just that, assigning the result to a new prefs value:

 private​ ​val​ prefs =
  PreferenceManager.getDefaultSharedPreferences(application)

Use the Fast AI Preference

We can now use prefs to get the preference values as needed. It’s out of order for how we set things up in SettingsFragment, but let’s start with the Fast AI setting since it’s a one-line change.

Down in the playAITurn function, we currently have a 1000 millisecond ...

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.