How can a PreferenceActivity use a custom preference file

一笑奈何 提交于 2019-12-03 01:29:07

You could read all the preferences at the beginning of your app, and then store them in the Preferences using

Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
e.putBoolean("yourPreference", true);
e.putString("yourOtherPreference", "This is the Value");
...
e.commit();

I hope that helps

emmanuel.aquino

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

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.

Maaalte is correct, what you want to do is onCreate test for the existence of your custom file and if it's there, rename it to standard shared preferences filename.

Another option is to read your old prefs one-by-one and use the shared preferences API to add them as you read them and then delete your old prefs when you are done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!