I would like to know the scope of
SharedPreferences
. I want to set some variables which can be used in entire application all the time untill unless the application uninstalled by the user. Can I use SharedPreferences to save the value?? I know that I can use database, But I want to know the scope of SharedPreferences, so that I can use SharedPreferences properly. Somebody please help, Thanks in advance.
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).
You can use SharedPreferences. If you curious about the life of SharedPreferences then its clear that it will be available for entire life of your application. But keep in mind that All application storage looses data when user clears the application data, so that time SharedPreferences will also loose the value.
Instead of using db, you can use SharedPreferences.
Ref : data-storage
you can use SharedPreferences to save variables within your application.
As a example you can use SharedPreferences in a game to save the score/points.
In this case if you are dealing with less number of variables i think the best way is
SharedPreferences.
Hope my answer helped you.
It is basically use small amount of data in key value pair form. And it will store all premitive type data with key value pair.
it is basically use to save password. session key, authuntication key etc.
SharedPreferences are stored in an .xml file, placed in your app's private data area.
The file's path is something like:
/data/data/PACKAGE_NAME/shared_prefs/PREFS_NAME.xml
and will be deleted only when the user uninstalls your app or clears the app data through Settings > Apps.
Can I use SharedPreferences to save the value?
Yes, you can use SharedPreferences to save any primitive value and retrieve it later on using the same key.
Parallel to SharedPreferences there's a persistance data which is not shared in the whole application but in an single Activity: Preferences. This is usefull to store values like filters parameters or temporary configurations.
Example from doc:
// SharedPreferences
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
// Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Related
I am designing a simple application that will count how many times a user has tapped on a imageView. My question is what would be the best way of saving and reading this file. Any suggestions? I am thinking something like using Parse.com's local database. I have tried it, but I could not get it working the way I wanted. I am still a beginner, so please not so fancy suggestions.
Try to save data in SharedPreference. SharedPreference works like database for application on device that will be stored until any one has unistall app from device.
To create sharedPrefernce-
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To store data -
prefs.edit().putInt("key", int_value).apply();
To retrieve data-
// use a default value
int l = prefs.getLong("key", default_value);
Simplest options is always thebest option, go with shared preferences
Here is simple tutorial from google http://developer.android.com/training/basics/data-storage/shared-preferences.html
It will store your data in application local file. Take a note of that there are different shared preferences in example getPreferences() will return file specific for activity you used this method. While getSharedPreferences() will return application global file.
How is it possible to save a variable value in an Android app? Can the value be saved in memory? I am planning to save a float value in my application, so the next time the app is opened, the previous value will be loaded. How do I go about this? Shared Preferences or something else?
Yes. SharedPreferences is the best available option.
To store float value:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat("storedFloat", storedFloatPreference); // value to store
editor.commit();
Also check this: http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putFloat(java.lang.String,%20float)
Check this SO question. It nicely answers your question: How do I get the SharedPreferences from a PreferenceActivity in Android?
Yes its possible use shared preferences.
http://developer.android.com/reference/android/content/SharedPreferences.html
http://developer.android.com/guide/topics/data/data-storage.html#pref
You can also use sqlite data base to store the data and retrieve when you need it.
Your other storage options. you could store your values to a file in memory.
http://developer.android.com/guide/topics/data/data-storage.html
Look at the Storage Options Docs to be sure you pick the most appropriate type for you but if its just one value then SharedPreferences should work fine for you.
See The Docs for a good example on how to use SharedPreferences
I would like to be able to save my users session or sharedPrefrences in a way that if the user kills the application and you start it it would look like this.
Button one = Start Activity with Blank Preferences
Button Two = List of Saved Sessions of Preferences and once clicked all put into the Starting activity.
Is this possible and if so how would I go about doing that?
Thank you!
Yes you can do that and it is good to use sharedPreferences if you just have to store some session variables. But if it is more, then go for database.
Do clear sharedPrefences in your application you need to do this:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
Editor editor = settings.edit();
editor.clear();
editor.commit();
For reading the preferences, you can keep a sharedPreference with the count for the seesions. While saveing the prefences, always save with the strings session1, session2, session3 etc. So, while accessing them based on count, prepare a loop and form the string and access all the session variables and show them.
The reason why I didnt suggest you to do getAll() for sharedPreference is that, you may save few other things in sharedPreference. So by forming strings yourself, while reading you can just get the sessions and not other data saved in sharedPreference.
I hope you understand what I meant
Is this possible
I would say yes, depending on exactly what you mean.
if so how would I go about doing that?
SharedPreferences has a couple different functions to do something like this, depending on exactly what you want. You can get a Map of all preferences that are stored after clicking Button2 with getAll() or a set of preferences with a certain String such as "userName" or something similar with getStringSet(). Play around with the functions it offers and see if it gives you what you are looking for.
Also take not of the warnings of these functions
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
I'm novice in Android development and now I'd really like to learn Shared Preferences. I've googled it so many times and I don't think I quite mastered it.
I believe this Shared Preferences will help me to store username and a password on my login screen activity. Thanks you!
I made some videos about this as an audition for a job. They helped me get the job, and they're still available on Vimeo, so hopefully they can help you out.
Part 1: Saving Data
Part 2: Retrieving Data
Also, for what it's worth, be careful storing usernames and passwords in SharedPreferences. You can do it there, but read up on the risks in this other SO question: What is the most appropriate way to store user settings in Android application
For android, there are primarly three basic ways of persisting data:
shared preferences to save small chunks of data
tradition file systems
a relational database management system through the support of SQLite databases
SharedPreferences object help you save simple application data as a name/value pairs - you specify a name for the data you want to save, and then both it and its value will be saved automatically to an XML file for you. To save a data in sharedPreferences file:
Obtain an instance of sharedPreferences file:
SharedPreferences appPrefs = getSharedPreferences( or fileName, MODE_PRIVATE);
Create SharedPreferences.Editor object
To put for example a String value into SharedPreferences object use putString() method.
To save the changes to the preferences file, use the commit() method
That will look like this:
// obtain an instance of the SharedPreferences class
preferences = getSharedPreferences(prefFileName, MODE_PRIVATE);
editor = preferences.edit();
// save username String
editor.putString("username", student).commit();
To retrieve it use getString() method:
preferences.getString(username, null) where null is a default value that will be returned if username key is not found in the file.
I am pretty new to Android dev. I am browsing through the API here http://developer.android.com/reference/android/content/SharedPreferences.html
but I am confused about how to actually get the file contents and read or write from them.
I have this code to get the SharedPreferences object:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( MyClassName.this);
but in this case, I get the reference to sharedPreferences, but not a connection to my file which stores the preferences data.
Maybe I am not understanding the API correctly, but how am I supposed to get the reference to the file and read/write to it?
Thanks!
If you want to get values from your SharedPreferneces you have to use (this example works with Strings, but you can also call getBoolean, getInt, etc..)
prefs.getString("myString", "defaultValue"); // "defaultValue" will be returned in case "myString" wasn't saved on the SharedPreferences
to store some values you can do it like this:
prefs.edit()
.putString("myString", "newValue")
.putBoolean("working", true)
.commit();
As you can see you can edit more than one value at once..
edit() will return you an editor that you have to use in order to modify the sharedpreferences file, and when you end to edit it call commit() in order to make the changes permanently
but in this case, I get the reference to sharedPreferences, but not a connection to my file which stores the preferences data.
The SharedPreferences object has a "connection" to the file which stores the preference data.
Maybe I am not understanding the API correctly, but how am I supposed to get the reference to the file and read/write to it?
To read preferences, use the getters on SharedPreferences (e.g., getString()). To write preferences yourself:
Get a SharedPreferences.Editor by calling edit() on the SharedPreferences object
Use the setters on the Editor (e.g., putString())
Call apply() (where possible) or commit() on the Editor to save your changes
In addition, you can (and in many cases should) also use a PreferenceActivity to allow users to directly view and modify their preferences.