A set of questions about SharedPreferences that I was looking for:
What, Why, When?
How does it work inside?
The Best Practice to use it?
Only some of those question were answered here. That's why I made some investigations, and tests.
As I've answered on my own question I've decided to share answer with other people.
I've wrote a little article that can also be found here.
Best Practice: SharedPreferences
Android provides many ways of storing application data. One of those ways leads us to the SharedPreferences object which is used to store private primitive data in key-value pairs.
All logic are based only on three simple classes:
SharedPreferences
SharedPreferences.Editor
SharedPreferences.OnSharedPreferenceChangeListener
SharedPreferences
SharedPreferences is main of them. It's responsible for getting (parsing) stored data, provides interface for getting Editor object and interfaces for adding and removing OnSharedPreferenceChangeListener
To create SharedPreferences you will need Context object (can be an application Context)
getSharedPreferences method parses Preference file and creates Map object for it
You can create it in few modes provided by Context, it's strongly recommended to use MODE_PRIVATE because creating world-readable/writable files is very dangerous, and likely to cause security holes in applications
// parse Preference file
SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// get values from Map
preferences.getBoolean("key", defaultValue)
preferences.get..("key", defaultValue)
// you can get all Map but be careful you must not modify the collection returned by this
// method, or alter any of its contents.
Map<String, ?> all = preferences.getAll();
// get Editor object
SharedPreferences.Editor editor = preferences.edit();
//add on Change Listener
preferences.registerOnSharedPreferenceChangeListener(mListener);
//remove on Change Listener
preferences.unregisterOnSharedPreferenceChangeListener(mListener);
// listener example
SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener
= new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
};
Editor
SharedPreferences.Editor is an Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()
Use simple interface to put values in Editor
Save values synchronous with commit() or asynchronous with apply which is faster. In fact of using different threads using commit() is safer. Thats why I prefer to use commit().
Remove single value with remove() or clear all values with clear()
// get Editor object
SharedPreferences.Editor editor = preferences.edit();
// put values in editor
editor.putBoolean("key", value);
editor.put..("key", value);
// remove single value by key
editor.remove("key");
// remove all values
editor.clear();
// commit your putted values to the SharedPreferences object synchronously
// returns true if success
boolean result = editor.commit();
// do the same as commit() but asynchronously (faster but not safely)
// returns nothing
editor.apply();
Performance & Tips
SharedPreferences is a Singleton object so you can easily get as many references as you want, it opens file only when you call getSharedPreferences first time, or create only one reference for it.
// There are 1000 String values in preferences
SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 4 milliseconds
SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds
SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds
As SharedPreferences is a Singleton object you can change any of It's instances and not be scared that their data will be different
first.edit().putInt("key",15).commit();
int firstValue = first.getInt("key",0)); // firstValue is 15
int secondValue = second.getInt("key",0)); // secondValue is also 15
When you call get method first time it parses value by key and adds this value to the map. So for second call it just gets it from map, without parsing.
first.getString("key", null)
// call time = 147 milliseconds
first.getString("key", null)
// call time = 0 milliseconds
second.getString("key", null)
// call time = 0 milliseconds
third.getString("key", null)
// call time = 0 milliseconds
Remember the larger the Preference object is the longer get, commit, apply, remove and clear operations will be. So it's highly recommended to separate your data in different small objects.
Your Preferences will not be removed after Application update. So there are cases when you need to create some migration scheme. For example you have Application that parse local JSON in start of application, to do this only after first start you decided to save boolean flag wasLocalDataLoaded. After some time you updated that JSON and released new application version. Users will update their applications but they will not load new JSON because they already done it in first application version.
public class MigrationManager {
private final static String KEY_PREFERENCES_VERSION = "key_preferences_version";
private final static int PREFERENCES_VERSION = 2;
public static void migrate(Context context) {
SharedPreferences preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
checkPreferences(preferences);
}
private static void checkPreferences(SharedPreferences thePreferences) {
final double oldVersion = thePreferences.getInt(KEY_PREFERENCES_VERSION, 1);
if (oldVersion < PREFERENCES_VERSION) {
final SharedPreferences.Editor edit = thePreferences.edit();
edit.clear();
edit.putInt(KEY_PREFERENCES_VERSION, currentVersion);
edit.commit();
}
}
}
SharedPreferences are stored in an xml file in the app data folder
// yours preferences
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
// default preferences
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
Android guide.
Sample Code
public class PreferencesManager {
private static final String PREF_NAME = "com.example.app.PREF_NAME";
private static final String KEY_VALUE = "com.example.app.KEY_VALUE";
private static PreferencesManager sInstance;
private final SharedPreferences mPref;
private PreferencesManager(Context context) {
mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static synchronized void initializeInstance(Context context) {
if (sInstance == null) {
sInstance = new PreferencesManager(context);
}
}
public static synchronized PreferencesManager getInstance() {
if (sInstance == null) {
throw new IllegalStateException(PreferencesManager.class.getSimpleName() +
" is not initialized, call initializeInstance(..) method first.");
}
return sInstance;
}
public void setValue(long value) {
mPref.edit()
.putLong(KEY_VALUE, value)
.commit();
}
public long getValue() {
return mPref.getLong(KEY_VALUE, 0);
}
public void remove(String key) {
mPref.edit()
.remove(key)
.commit();
}
public boolean clear() {
return mPref.edit()
.clear()
.commit();
}
}
Yakiv has mentioned everything about preferences so neatly and nicely. I just want to add one more point. While editing data in shared preferences we usually do
mPref.edit()
which creates a new object of SharedPreferences.Editor type every time which can result in unnecessary objects in memory. So you can maintain a reference to editor object too and save memory and object creation time and corresponding garbage collection time.
Related
My sharedPreferences does not persist after I close the app. It always leaves the default 4 that I add the first time the app runs.
static public Boolean addFavoriteItem(Integer itemId, Context c) {
SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(c);
Set<String> list = new HashSet<>();
list = s.getStringSet("favItems",list);
list.add(Integer.toString(itemId));
s.edit().putStringSet("favItems",list).apply();
Log.d("listNowAdd:",list.toString());
return true;
}
static public Boolean removeFavoriteItem(Integer itemId, Context c) {
SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(c);
Set<String> list = new HashSet<>();
list = s.getStringSet("favItems",list);
list.remove(Integer.toString(itemId));
s.edit().putStringSet("favItems",list).apply();
Log.d("listNowRemove:",list.toString());
return true;
}
It does work temporarily, even after the activity is restarted, but not after the app is closed and reopened, any ideas?
You cannot modify the StringSet returned by SharedPreferences as documented:
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.
Essentially what happens is that you're modifying the Set owned by SharedPreferences. It "works" while the app is running since the same (modified by you) Set is kept in memory. When you're trying to save the changes, shared preferences implementation compares the values using equals() and of course the set is equal with itself, and therefore no changes are actually saved.
You can create a new Set<String> e.g. HashSet<String> and addAll() the set you retrieved from shared preferences and do your modifications on this copy.
SharedPreferences doesn't work correct in one existing apps. I tried many different ways but still not working. Always get default values app start again.
It's working when I use same code in created new app.
It's working all of other existing apps.
Do you know why?
String default_user = "Default_User";
SharedPreferences pref = this.getSharedPreferences("TEST_SHAREDPREF", MODE_PRIVATE);
String user = pref.getString("user", default_user);
Log.d("SHARED CHECK", user);
if (user.equals(default_user)) {
SharedPreferences.Editor edit = pref.edit();
edit.putString("user", "new_user");
boolean ok = edit.commit();
user = pref.getString("user", default_user);
Log.d("SHARED WRITE", user);
Toast.makeText(this, user + " Save process: " + ok, Toast.LENGTH_LONG).show();
} else {
Log.d("SHARED READ", user);
Toast.makeText(this, "READ SharedPrefs: " + user, Toast.LENGTH_LONG).show();
}
EDIT: log results
that block always return this for which is incorrect app and I don't know why
//first run
SHARED CHECK Default_User
SHARED WRITE new_user
//each time after first
SHARED CHECK Default_User
SHARED WRITE new_user
That block always return this for which are all apps
//first run
SHARED CHECK Default_User
SHARED WRITE new_user
//each time after first
SHARED CHECK new_user
SHARED READ new_user
When you call apply() or commit() the changes are first saved to the app's memory cache and then Android attempts to write those changes onto the disk. What is happening here is that your commit() call is failing on the disk but the changes are still made to the app's memory cache, as is visible in the source.
It is not enough to read the value from the SharedPreferences as that value might not reflect the true value that is on the disk but only that stored in the memory cache.
What you are failing to do is to check the boolean value returned from the commit() call, it is probably false for your problematic case. You could retry the commit() call a couple of times if false is returned.
Just use below method and check.
Create one Java class AppTypeDetails.java
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class AppTypeDetails {
private SharedPreferences sh;
private AppTypeDetails() {
}
private AppTypeDetails(Context mContext) {
sh = PreferenceManager.getDefaultSharedPreferences(mContext);
}
private static AppTypeDetails instance = null;
public synchronized static AppTypeDetails getInstance(Context mContext) {
if (instance == null) {
instance = new AppTypeDetails(mContext);
}
return instance;
}
// get user status
public String getUser() {
return sh.getString("user", "");
}
public void setUser(String user) {
sh.edit().putString("user", user).commit();
}
// Clear All Data
public void clear() {
sh.edit().clear().commit();
}
}
Now Set value to SharedPreferences.
AppTypeDetails.getInstance(MainActivity.this).setUser(<user name>);
Get Value form SharedPreferences.
String userName = AppTypeDetails.getInstance(MainActivity.this).getUser();
Now do any thing with the userName.
Always check
if(userName.trim().isEmpty())
{
// Do anything here.
}
because In SharedPreferences we set user name blank ("")
or
you can set user name null in SharedPreferences then you need check
if(userName != null){
//do anything here
}
For clear data from SharedPreferences.
AppTypeDetails.getInstance(MainActivity.this).setUser("");
or
AppTypeDetails.getInstance(MainActivity.this).clear();
One thing you could try is to get a new SharedPreference instance after committing and see what happens:
SharedPreferences pref = this.getSharedPreferences("test", MODE_PRIVATE);
String user = pref.getString("user", default_user);
if (user.equals(default_user)) {
pref.edit().putString("user", "new_user").commit();
SharedPreferences newPref = this.getSharedPreferences("test", MODE_PRIVATE);
user = newPref.getString("user", default_user);
}
Your editor is committing a new preference map into disk, but it is possible that the old SharedPreference instance is not notified of the change.
Instead of using edit.commit();, you should use edit.apply();. Apply
will update the preference object instantly and will save the new
values asynchronously, so allowing you to read the latest values.
Source - Also read detailed difference between commit() and apply() in this SO Post.
The "Source" post referenced above also states pretty much the same problem and switching to apply() seems to have resolved the issue there.
Problem statement in the referenced post:
The problem is that when I am accessing this values, it is not
returning updated values, it gives me a value of SharedPreferences.
But when I am confirming the data in XML file ,the data updated in
that.
PS: And the reason that the same code block is not working on this one app and working on all other apps could also be that you are either using the block at different places or updating the value somewhere else too.
Your code is right, the code works on any app very well, looks like that in some part of your app the shared preferences are been modified, the only way to find a solution is review all your code, because if this problem only happens on one app, it's somewhere on your app that the shared preferences are been modified, for good practices, you should have only one file class for the management of your preferences on that way you can comment or find usage for a method and you can find where the shared preferences was been modified.
BTW the best way to store an user, password, or any account info is using Account Manager.
For good practices you can see this sample PreferenceHelper class.
public class PreferencesHelper {
public static final String DEFAULT_STRING_VALUE = "default_value";
/**
* Returns Editor to modify values of SharedPreferences
* #param context Application context
* #return editor instance
*/
private static Editor getEditor(Context context){
return getPreferences(context).edit();
}
/**
* Returns SharedPreferences object
* #param context Application context
* #return shared preferences instance
*/
private static SharedPreferences getPreferences(Context context){
String name = "YourAppPreferences";
return context.getSharedPreferences(name,
Context.MODE_PRIVATE);
}
/**
* Save a string on SharedPreferences
* #param tag tag
* #param value value
* #param context Application context
*/
public static void putString(String tag, String value, Context context) {
Editor editor = getEditor(context);
editor.putString(tag, value);
editor.commit();
}
/**
* Get a string value from SharedPreferences
* #param tag tag
* #param context Application context
* #return String value
*/
public static String getString(String tag, Context context) {
SharedPreferences sharedPreferences = getPreferences(context);
return sharedPreferences.getString(tag, DEFAULT_STRING_VALUE);
}}
I used the below code pattern in one of my app and I always get the latest value stored in SharedPreferences. Below is the edited version for your problem:
public class AppPreference
implements
OnSharedPreferenceChangeListener {
private static final String USER = "User";
private static final String DEFAULT_USER = "Default_User";
private Context mContext;
private String mDefaultUser;
private SharedPreferences mPref;
private static AppPreference mInstance;
/**
* hide it.
*/
private AppPreference(Context context) {
mContext = context;
mPref = PreferenceManager.getDefaultSharedPreferences(context);
mPref.registerOnSharedPreferenceChangeListener(this);
reloadPreferences();
}
/**
* #param context
* #return single instance of shared preferences.
*/
public static AppPreference getInstance(Context context) {
return mInstance == null ?
(mInstance = new AppPreference(context)) :
mInstance;
}
/**
* #return value of default user
*/
public String getDefaultUser() {
return mDefaultUser;
}
/**
* Set value for default user
*/
public void setDefaultUser(String user) {
mDefaultUser = user;
mPref.edit().putString(USER, mDefaultUser).apply();
}
/**
* Reloads all values if preference values are changed.
*/
private void reloadPreferences() {
mDefaultUser = mPref.getString(USER, DEFAULT_USER);
// reload all your preferences value here
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
reloadPreferences();
}
}
You can set the value as below in your activity:
AppPreference.getInstance(this).setDefaultUser("user_value");
To get the updated value for saved user, use below code:
String user = AppPreference.getInstance(this).getDefaultUser();
Hope this solution will fix the problem you are facing.
For saving and retrieving data from SharedPrefrence create a util type methods in your Utility class:
Below I have given code snippet for both the methods i.e for saving and retrieving SharedPrefrence data:
public class Utility {
public static void putStringValueInSharedPreference(Context context, String key, String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String getStringSharedPreference(Context context, String param) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(param, "");
}
}
Now you just need to call Utility.putStringValueInSharedPreference(Activity_Context,KEY,VALUE); to put data in prefrence and Utility.getStringSharedPreference(Activity_Context,KEY); to get value from prefrence.
This methodology reduces chances of error; May this will be helpful for you.
I recommend controlling your preference check with another preference value.
String user = pref.getString("user", default_user);
In your preference check:
String getStatus = pref.getString("register", "nil");
if (getStatus.equals("true")) {
// move on code stuff
}
else{
// ask for preferences
}
In the part where the preference is added.
edit.putString("user", "new_user");
editor.putString("register", "true");
edit.commit;
Don't know if this is correct or not, but you can implement a Shared preference change listener and you can log in that and check if the shared preference is not called again in any other class, which is again setting the value to Default user.
I Mean you got to make sure that there is no other code resetting the value of user.
The code itself looks fine and you say it works in other apps. Since you have confirmed the XML file gets properly created, the only remaining option is that something happens to the file before you read it during app launch. If there is no other code in the app that handles any shared preferences, then something must wipe the file during the launch.
You should take careful look at launch configuration of this particular app and compare to other apps where this is working. Maybe there is some option that wipes user data when app is started. I know there is such an option at least for emulator (in Eclipse), but I am not sure if you are running this in emulator or device.
You should also try launching the app directly from the device instead of IDE. This tells if the problem is in the app itself or IDE configuration. And try moving this from onCreate (where I presume this is) to onResume and compare does it work when you resume to the app versus when you completely restart it.
Finally I solved that problem.
A method was added by one of other developers.
All files under data/data/packageName folder except libs folder were being deleted by this method.
I think they tried delete cache folder.
Removed this method and it solved.
Every time my app starts an ID is generated by my program. Since each time it generates a different ID , I want to save the ID somewhere before exiting the program so I can use it again when the program runs later.
Sample ID : 125469
Note : I rather to not save it in file but some else.
Even a better solution would be to use GreenDAO here are some examples
https://github.com/greenrobot/greenDAO
https://github.com/octa-george/Android-GreenDao-Sample
GreenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases. Being highly optimized for Android, greenDAO offers great performance and consumes minimal memory.
You only need the greendao jar
public class MyDaoGenerator {
public static void main(String args[]) throws Exception {
Schema schema = new Schema(1, "ro.octa.greendaosample.dao");
createSchema(schema);
new DaoGenerator().generateAll(schema, "C:/GreenDaoSchema");
}
private static void createSchema(Schema schema) {
Entity user = schema.addEntity("DBUser");
user.addIdProperty().primaryKey().autoincrement();
user.addStringProperty("email").notNull().unique();
user.addStringProperty("password").notNull();
}
After a few lines of code you have the model generated for autoincrement id, email and password.
You can use SharedPreferences or you can use SQLite database for storing purposes.
You can do it like that:
private SharedPreferences prefs;
...
...
private void initializeFromPreference() {
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
myId = prefs.getInt("myId", myId );
}
...
...
public void onDestroy() {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("myId", myId );
editor.commit();
super.onDestroy();
}
i wouldnt use a database for a single value, Sharedpreferences is a good way to do so
You can Use SharedPreferences to store your id
SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey", Context.MODE_PRIVATE);
Editor edit=prefs.edit();
//to store value
edit.putInt("Your Id Name", Id Value).commit();
//to get value
int id=prefs.getInt("Your Id Name", 0);
Here's the simple way, I can think of :)
public static SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("MyIds", Context.MODE_PRIVATE);
if (sharedpreferences.contains("id"))
{
String id = sharedpreferences.getString("id", "");
// *Use existing id and enjoy* ;)
}
else{
// *Generate New ID (yourID)*
Editor editor = sharedpreferences.edit();
editor.putString("id", yourID);
editor.commit();
}
Is the following the intended/reasonable way to commit and rollback SharedPreferences, and to use it in general?
public class Settings {
private static final String PREFS_NAME = "Settings";
private static SharedPreferences preferences = null;
private static SharedPreferences.Editor editor = null;
public static void init(Context context) {
// Activity or Service what ever starts first provides the Context
if (preferences == null)
// getSharedPreference because getPreferences is a method of Activity only (not Service or Context)
preferences = context.getSharedPreferences(PREFS_NAME, 0);
editor = preferences.edit();
}
public static String getEmail() {
return preferences.getString("email", null);
}
public static void setEmail(String email) {
editor.putString("email", email);
}
public static String getPassword() {
return preferences.getString("password", null);
}
public static void setPassword(String password) {
editor.putString("password", password);
}
public static void save() {
editor.commit();
}
public static void rollback() {
editor = preferences.edit();
}
}
This is more detail as enforced by stackoverflow editor. I really don't know what else should be said about this.
Experts' feedback is much appreciated. And if may snipped is reasonable it could well as better explanation then all other threads I have found here.
There is only one change in the following method from my understanding. Because if u forget to initialize preference ,u will get null pointer exception.
public static void rollback(Context context) {
if (preferences == null)
// getSharedPreference because getPreferences is a method of Activity only (not Service or Context)
preferences = context.getSharedPreferences(PREFS_NAME, 0);
editor = preferences.edit();
}
Best way to have things persisted is "USE OF PREFERENCE ACTIVITY". See examples and read online docs about them. Use EditTextPreference for automatically persisting values.
First thing is that no body ever uses shared preference to save user id and password . Because shared preference is key-value pair. For key Email you can have only one respective value. Here what you want is :- for key Email multiple values and for key password also multiple value.
There exist one solution if u want to do something like this. Use email id (xyz#xyz.com) as key . And password as the value of key(xyz#xyz.com).
I have an Activity with some Fragments and have to persist some fair amount of data but not fair enough for SQL. Now what is the best practice in use of SharedPreferences? I wanna avoid calls to the file and the commits of it as much as possible. Because I assume parsing of that file and especially the commits are bad for performance.
I know this question, which says that the call on the SharedPreferences file always returns the same object. But what about the commit?
Should I use f.e. a Bundle to save my data and persist them at once when the Activity goes in the background? Or should I always persist a portion of my data like in every Fragment? Or I just hunting ghosts?
I think this is an unnecessary and premature optimization that won't realistically have any performance impact. How much data are you storing in SharedPreferences? I think you are just hunting ghosts.
If you are using it as a means of communication between fragments then you are using it for an unintended purpose.
edit: For some further evaluation, SharedPreferences basically stores things in a Key/Value map. This makes it really convenient to store and retreive simple things such as user preferences (hence the name). If you need to do anything more complex than that, you can quickly see how cumbersome it would become using a Key/Value map, which is why it would make sense to move to a database storage like SQLite. With a database you get the obvious benefit of using queries. Basically the point of SharedPreferences is added convenience to developers so that you don't need to create a full database to store simple values. See here for more:
Pros and Cons of SQLite and Shared Preferences
Not sure what "fair amount of data" really is, but use SQL - that's why it is here for. I do really so no excuse to not do so, knowing how really easy it is. If you never tried sqlite on android (which could explain why you want to try to avoid it :) then go thru elementary tutorial and you are really done.
You can use a public class and we have to call it's method whenever we required.
example:
SessionManager mSessionManager = new SessionManager(this);
mSessionManager.putStringData("key", "value");
The Class is given below:
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int MODE_MULTI_PROCESS = 0;
// Sharedpref file name
private static final String PREF_NAME = "SharedPref_Name";
private SharedPreferences getPref() {
return _context.getSharedPreferences(PREF_NAME, Context.MODE_MULTI_PROCESS);
}
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, MODE_MULTI_PROCESS);
editor = pref.edit();
}
/**
* Set the String data in the preferences.
*/
public void putStringData(String keyname, String value) {
editor.putString(keyname, value);
editor.commit();
}
/**
* #return the string data from the prefs
*/
public String getStringData(String keyName) {
return pref.getString(keyName, "");
}
/**
* Set the int data in the preferences.
*/
public void putIntData(String keyname, int value) {
editor.putInt(keyname, value);
editor.commit();
}
/**
* #return the boolean data from the prefs
*/
public int getIntData(String keyName) {
return pref.getInt(keyName, 0);
}
/**
* Set the boolean data in the preferences.
*/
public void putBooleanData(String keyname, boolean value) {
editor.putBoolean(keyname, value);
editor.commit();
}
/**
* #return the boolean data from the prefs
*/
public boolean getBooleanData(String keyName) {
return pref.getBoolean(keyName, false);
}
/**
* Set the long data in the preferences.
*/
public void putLongData(String keyname, long value) {
editor.putLong(keyname, value);
editor.commit();
}
/**
* #return the long data from the prefs
*/
public long getLongData(String keyName) {
return pref.getLong(keyName, 99);
}
/**
* remove data from pref
*
* #param keyName
*/
public void removeData(String keyName) {
editor.remove(keyName);
editor.commit();
}
//Save arrayList of Model type
public void saveAssignedLocationsToSharedPrefs(List<Locations> LocationModel) {
Gson gson = new Gson();
String jsonLocation = gson.toJson(LocationModel);
editor.putString("LocationArray", jsonLocation);
editor.commit();
}
//get arrayList of Model type
public ArrayList<Locations> getAssignedLocationsFromSharedPrefs() {
List<Locations> LocationData;
String jsonLocation = pref.getString("LocationArray", null);
Gson gson = new Gson();
Locations[] LocationItems = gson.fromJson(jsonLocation,
Locations[].class);
LocationData = Arrays.asList(LocationItems);
LocationData = new ArrayList<Locations>(LocationData);
return (ArrayList<Locations>) LocationData;
}
}