Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 481
zooter Avatar asked Apr 21 '16 18:04

zooter


People also ask

Is SharedPreferences Singleton?

I've noticed that a lot of projects have their SharedPreferences code scattered all over the project. The reason for this mostly is that fetching SharedPreference and reading/writing preferences as and when needed is the easiest thing to do when writing an app.

What is difference between preferences and SharedPreferences?

Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML. Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you.

What is SharedPreferences example?

Shared Preferences can be thought of as a dictionary or a key/value pair. For example, you might have a key being “username” and for the value, you might store the user's username. And then you could retrieve that by its key (here username).

Is SharedPreferences deprecated?

getSharedPreferencesMode. Deprecated: Deprecated in Java. Returns the current mode of the SharedPreferences file that preferences managed by this will use. The mode that can be passed to Context#getSharedPreferences(String, int) .

What is the use of shared preferences?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage. Shared Preferences can be thought of as a dictionary or a key/value pair.

How to create a shared preferences file for an app?

The first thing we need to do is to create one shared preferences file per app. So name it with the package name of your app- unique and easy to associate with the app. When you want to get the values, call the getSharedPreferences () method. Shared Preferences provide modes of storing the data (private mode and public mode).

How do I get the shared preferences in Java?

In order to use shared preferences, you have to call a method getSharedPreferences () that returns a SharedPreference instance pointing to the file that contains the values of preferences. SharedPreferences sharedpreferences = getSharedPreferences (MyPREFERENCES, Context.MODE_PRIVATE);

How do I write to sharedpreferences?

Writing into SharedPreferences is simple, as you probably expect. Since the idea of this whole approach is simple storage of simple data. But it starts by acquiring an Editor. And then you can insert data by calling the appropriate put method (i.e. putString () to store a String value).


3 Answers

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.

like image 69
Francesc Avatar answered Sep 21 '22 14:09

Francesc


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.

like image 40
Shadab Ansari Avatar answered Sep 18 '22 14:09

Shadab Ansari


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.

like image 41
batero Avatar answered Sep 19 '22 14:09

batero