Listing 26.11 - deprecated PreferenceManager and getDefaultSharedPreferences

Hi,

after creating QueryPreferences.kt file android.preference.PreferenceManager is imported by default and Android Studio warns that PreferenceManager and getDefaultSharedPreferences are deprecated.

Here is the solution:

  1. add two dependencies to your gradle
implementation 'androidx.preference:preference-ktx:1.2.1'
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
  1. delete
    import android.preference.PreferenceManager
    add
    import androidx.preference.PreferenceManager

If you are facing “duplicate class” run-time error, you might also need to add these dependencies:

implementation "androidx.lifecycle:lifecycle-viewmodel:2.5.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
1 Like