I’m using SharedPreferences to store my data across all Activities within my application. I get access to it like this:
SharedPreferences mSharedPreferences = getSharedPreferences("MyPrefs", 0);
I’ve implemented PreferenceActivity so users can change values through it but it happens it reads/writes data not to “MyPrefs” but to:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Which is a bit unexpected for me. Is that possible to force PreferenceActivity to deal with my “MyPrefs” preferences? And what is the point to have several preferences within single application? Thank you.
That’s fine, so long as you weren’t planning on using
PreferenceActivity.Oops.
Also, get rid of
getApplicationContext()there, unless you have a specific reason for using theApplicationrather than the activity/service/whatever. Only use theApplicationobject when you have to and you know why you have to.Not readily. Unless you have some specific reason to invent your own
SharedPreferencesfile, I’d use the default one.You might have a reusable library or component want to store stuff in
SharedPreferences, and that might have its own file so as not to mess up any preferences from the hosting application. That being said, multiple preference files is not typically needed.