SharedPreferences: What are they shared with? - android

I find the name of stored preferences on Android (SharedPreferences) to be a tad confusing. It was probably chosen with a purpose, so how exactly are these preferences shared? e.g. I don't want other applications accessing my app's information willy-nilly.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
My imagination regarding the use of these things is still pretty limited.

It was probably chosen with a purpose, so how exactly are these preferences shared?
They are shared among all components of your application (e.g., all of your activities).
e.g. I don't want other applications accessing my app's information willy-nilly.
SharedPreferences are private to your application by default.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
I am not quite certain what you are referring to, since you provided just bare method names with no classes. If you are intending on collecting preference values from the user via the preference screen system, use PreferenceManager.getDefaultSharedPreferences(). I generally use that for everything.

Sharedprefernce are basically key,value pairs with can are saved in an xml and is accessible by an application. You can use it to save some setting value or default values or any other form of key value pairs.
So a key,value pair can be saved in one activity can be accessed in other activity.
Different modes means that whether the data saved can be used by other application or not. You should not worry about all modes. As MODE_WORLD_READABLE & MODE_WORLD_WRITEABLE are deprecated now.
For the difference:-
Difference between getShared/get preference

Related

Multiple SharedPreferences

Actually, I have a doubt regarding SharedPreferences in android.
I have started learning Android a few days back and I am creating a SignUp Page for now, for that, I have searched the internet and got some ideas and now I am using multiple shared preferences in my code which I am thinking it would be a bad practice. So, I just wanna know, what happens to the Application if we create multiple shared preferences in the code.
This will just result in multiple SharedPrefenrences files (XML) in the data folder of your app. This is neither a problem nor a bad practice. If you have larger sets of structured data, consider using a database (e.g. SQLite/Room).
It is not bad practice at all. There is always a default shared preference.
we can get default shared pref filename using .getDefaultSharedPreferences() method.
you can get back up of any single shared pref file which is efficient.
With the concept of consuming more memory we shouldn't use SharedPreference to store large amounts of data, Alwayas use SQL DB in android for that. Multiple sharedpreference is good because , you can store data seperate for different sections within the app if it doesn't need to be shared. Shared preference is just a xml file with key value pair. So if you store only simple key value pairs, its okay to have multiple shared preferences. But be logical in your decision, dont just do that because you can

Does SharedPreferences name need to be unique?

Probably a dumb question, but I"m using a SharedPreference with a few different names "MyPrefs1", "MyPrefs2" etc.
I'm assuming this is restricted to my app. i.e. if some other app tries to use the same name, it won't overwrite my values
I pretty much believe my understanding is correct, but the name "SharedPreferences" seem to indicate that it can be shared between apps? (is it for Sharing between activities?)
That's correct, the SharedPreferences are stored in your app's private folder (to be exact, in /data/data/your package name/shared_prefs).
You can give them whatever name you want.
SharedPreferences represent the preferences which can be shared between different components of your application. The SharedPreferences you create in your application is never exposed with other applications.
Whether you use PreferenceManager.getDefaultSharedPreferences() or Context.getSharedPreferences("file_name", Context.MODE_PRIVATE); both are particular to your application only.
Note - SharedPreferences or Preferences is not exposed to other applications.
Although you assumption is right that the SharedPreferences are not shared between apps and thus names cannot clash, it's a good practice (besides of put a specific name to your shared preferences file) to add the package of your application as a prefix. This is useful if you have different flavours and you don't want to overwrite your preferences among them, especially when you're testing an app and don't want to screw up the original values of the shared preferences files.

Store user accounts data in preferences or sql... which is better, safer?

I'm creating an app that will need, at the most, probably 16 account's basic information. This includes several items per account such as name, password, and some basic settings. This information will be used periodically throughout the app to access information on the web, and display it to the user as needed. I will probably also be storing other information, per account, in a SQL file so it can be searched, categorized and displayed when requestedd.
I am going to use a Pref Fragment to add/edit each of these account's main information for simplicity (they are all identical in format, just different values) but as I started reading on the differences between shared preferences and SQL, there are alot of pros and cons for using each.
Where would the best place be to store this basic information?
Is this information, including passwords more secure in one vs the other from outside prying eyes?
Some people say that shared preferences can sometimes get corrupted... is that something I need to worry about?
I've never had problems with corrupted shared prefs. If you're only ever going to have around 16 account entries, I think shared prefs would be fine (and easier to implement than a Sqllite db). If you do use shared prefs, just make sure to hash or otherwise avoid saving passwords in plain text. Pref files, although normally restricted (if you specify MODE_PRIVATE) still can be accessed when a phone is rooted.

Android onSaveInstanceState for all instances of activity

After reading the documention it seems that onSaveInstanceStaate works per instance (as the name suggests). I am just wondering what the preffered method of storing data is so that it is available for all instances of that activity?
As MaciejGorski mentioned in his comment, there are different levels of data storage available in Android:
Shared preferences
Internal storage
External storage
SQLite database
Network
From personal experience, the lower you go down this list, the more complicated your implementation will become. Thus, if you are simply trying to save simple data for your app to be shared among different instances of an activity (or of multiple activities), shared preferences are certainly the way to go. You can even create private shared preferences, which only your app can access.
In any case, check out this SO answer for how to implement them: How to use SharedPreferences in Android to store, fetch and edit values

Android Settings Page

I'm working on building out a settings page for my Android app. For this I am using the PreferenceActivity and its functionality. The saving/editing of the settings is the simple part but I'm wondering what the best way to access the settings through the app is? I could of course access the SharedPrefs every time I need to, however, this seems inefficient to me...especially if since some of the settings could be access fairly frequently.
I'm wondering if it is worth creating a class that loads all of the settings when the app is opened and is then accessible through out the app via the Application class. This class would then be updated every time the settings are changed.
Is it worth taking this approach or is there a better way? Or is it not worth keeping the settings in memory throughout the lifetime of the app?
Thanks
The entire purpose for SharedPreference is what this is designed for. They are efficient to save/change the settings of your app, by just retrieveing it through your context. I dont see WHY you would keep it in memory, but you'd keep it in the XML key/value pairs that are SharedPrefereces.
Just use it, its what the android engineers would want you to do.
Just a snippet from the SDK:
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types. You can use SharedPreferences to save any primitive data:
booleans, floats, ints, longs, and strings. This data will persist
across user sessions (even if your application is killed).
By primitives, it means lightweight variables that can be stored/committed.

Categories

Resources