PreferenceManager getDefaultSharedPreferences deprecated in Android Q

后端 未结 6 2010
深忆病人
深忆病人 2021-01-30 04:32

PreferenceManager getDefaultSharedPreferences is deprecated in Android Q, how do I replace it?

6条回答
  •  难免孤独
    2021-01-30 05:25

    If you're just saving and retrieving key-value pairs you can replace:

     prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    

    with:

     prefs = getSharedPreferences(
                "my.app.packagename_preferences", Context.MODE_PRIVATE);
    

    Be sure to use the right file name for the new implementation or your users will lose access to everything saved with getDefaultSharedPreferences(!). The following will get the file name getDefaultSharedPreferences uses:

    getPackageName() + "_preferences"
    

提交回复
热议问题