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.
Related
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
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
I have an appliaction which will show a welcome window only the first time the user starts the app. What would be the best way to store this boolean variable (i.e. "isFirstTime") to phone storage? Should I use Shared Preferences or Internal Storage?
The docs say that if I use Internal Storage my "preference" file will automatically get removed upon uninstallation which is quite handy.
I want a clean, simple and fast solution.
Yes, I'd recommend using Shared Preferences. Basically you could put a shared preference with a key of "isFirstTime" and a type of boolean set to false. Then in your main activity do something like:
getBoolean (isFirstTime, true);
This, if it can't find isFirstTime will give you true, allowing you to do an if-statement based on the result.
I agree that SharedPreferences would probably be the most "clean, simple, and fast solution" that you are looking for. SharedPreferences are also deleted when the application is uninstalled.
Are the shared preferences associated with the App deleted when the app is removed?
SharedPreferences are your best option for this.
I have an Android application with more source packages. In the base package I have a PreferenceActivity used to configure the preferences of the app. From all the activities within the same package as the PreferenceActivity, I can access these preferences by using PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
However, from another package, activities use another shared preferences file when I call PreferenceManager.getDefaultSharedPreferences(getApplicationContext());. This stops me from accessing the preferences from the PreferenceActivity.
How can I solve this? I want to retrieve the same shared preferences in every package in the application.
Can't you just use a name for the shared preference and then get the same name for all the applications? Look under shared preferences at: http://developer.android.com/guide/topics/data/data-storage.html#pref
Edit: I think what you look after is here, no reason to take out parts as it gives a pretty good explanation: How do I get the SharedPreferences from a PreferenceActivity in Android?
As per my understanding the values stored using the SharedPreferences in android can be used only by the app which created it.(correct me if wrong).
Then what is the significance of the name SharedPreferences.
Is something really shared here?
well it can be shared accross applications if you set the mode to WORLD READABLE or WORLD_WRITEABLE. (think ringtone settings, notification message etc...)
see this link
public static final int MODE_WORLD_READABLE
Since: API Level 1
File creation mode: allow all other applications to have read access to
the created file.
The SharedPreferences use a secure /data directory for preferences that are shared within your app's activities and services.
http://developer.android.com/reference/android/content/SharedPreferences.html