How can a PreferenceActivity use a custom preference file

前端 未结 3 1394
谎友^
谎友^ 2021-02-05 06:43

My first attempt at preferences was without knowledge of PreferenceActivity. So now I have an app that stores all user preferences in a specific preference file.

3条回答
  •  猫巷女王i
    2021-02-05 07:02

    It may be too late to post this but you can find a nice solution here

    • How to make PreferenceActivity use non-default SharedPreferences

    You set the name of the default shared preferences file beforehand like this:

    public class MyPreferencesActivity extends PreferenceActivity {
        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
    
             PreferenceManager prefMgr = getPreferenceManager();
             prefMgr.setSharedPreferencesName("my_preferences");
             prefMgr.setSharedPreferencesMode(MODE_WORLD_READABLE);
    
             addPreferencesFromResource(R.xml.preferences);
        }
    }
    

    I hope this helps somebody.

    Regards.

提交回复
热议问题