I have an app making use of SharedPreferences
. One just stores app version to check against update for a changelog, the other contains some layout info that clear()
gets called on as the user chooses. I finally managed to get a PreferenceFragment
working and noticed a trend, so I thought I might ask this now before i go preference crazy (though I think I have enough).
I've done my best to search and see no specific mention of a problem, only that it's possible to have multiples.
I'm a little concerned about PreferenceManager.getDefaultSharedPreferences()
grabbing the wrong pref, but I might simply be misunderstanding the usage.
The only relevant code i could think of from my activity:
SharedPreferences storedVer = getSharedPreferences(VER_NUM, 0); SharedPreferences savedLayout = getSharedPreferences(LAYOUT_SAVE, 0);
Yes you can maintain as many shared preference files for an app as you can.
The SharedPreferences implementation in Android is thread-safe but not process-safe. Normally your app will run all in the same process, but it's possible for you to configure it in the AndroidManifest.
No. It can be easily hacked. If you want to put any sensitive data in shared prefrence file you can encrypt the data and store.
And more, it can be used in various ways such as Security. SharedPreferences data are not safe from hacking even if private-mode.
It is not bad practice at all. I think it is the opposite. I think different behaviours should use different sharedPreference
files.
.getDefaultSharedPreferences()
uses the default com.company.packagename.xml
file. And the others create their own files.
The followings advantages of using multiple sharedPreference's
come up in my mind.
sharedPreference
files to backup and restore. sharedPreference
file with that users private values in it. You may not want to delete some other.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With