Best practice handling SharedPreferences - android

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;
}
}

Related

Access SharedPreference inside a class that extends BroadcastReceiver

I'm trying to call my method(saveSharedpreferences.setCountReceived) from a class (SMSSendingWeb) that extends BroadcastReceiver, to a custom SaveSharedPreferences class.
My problem is it can't save into the SharedPreferences nothing happens.
I'm confused where is the problem of my code.
My argument in this method has error when
saveSharedpreferences.setCountReceived(SMSSendingWeb.this, countReceived);
I changed it to context has also error
saveSharedpreferences.setCountReceived(context, countReceived);
Here's my code: Please look into setCountReceived()
public class SaveSharedPreferences {
static final String PREF_USER_NAME= "username";
static final String PREF_COUNT_RECEIVED= "received";
static SharedPreferences getSharedPreferences(Context ctx) {
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
//==========================================================
public static void setCountReceived(Context ctx, Integer countReceived)
{
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
editor.putInt(PREF_COUNT_RECEIVED, countReceived);
editor.commit();
}
}
Android code:
public class SMSSendingWeb extends BroadcastReceiver {
RestService restService;
public static Integer countReceived = 0;
SaveSharedPreferences saveSharedpreferences;
#Override
public void onReceive(Context context, Intent intent) { //for Receiving
restService = new RestService();
saveSharedpreferences = new SaveSharedPreferences();
Bundle b = intent.getExtras();
Object[] pduObj= (Object[]) b.get("pdus");
String mobileno = null;
String message = null;
String data = null;
for(int i=0;i<pduObj.length;i++){
SmsMessage smsMessage=SmsMessage.createFromPdu((byte[]) pduObj[i]);
//get the sender number
mobileno=smsMessage.getOriginatingAddress();
//get the sender message
message=smsMessage.getMessageBody();
data="From : "+mobileno+"\n Message : "+message;
}
SMSInbox sms = new SMSInbox();
sms.MobileNo = mobileno;
sms.Message = message;
restService.getService().addSMS(sms, new Callback<String>() {
#Override
public void success(String s, Response response) {
//Toast.makeText(, "Successfully added", Toast.LENGTH_LONG).show();
countReceived += 1;
saveSharedpreferences.setCountReceived(SMSSendingWeb.this, countReceived);
Log.d("API inside restService", "count: " + countReceived);
}
#Override
public void failure(RetrofitError error) {
//Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
countReceived += 1;
Log.d("API outside restService", "count: " + countReceived);
saveSharedpreferences.setCountReceived(context, countReceived);
}
You are trying to access the Preferences and not Shared Preference there is a subtle difference between the two. As from this answer
Preferences is an Android lightweight mechanism to store and retrieve
pairs of primitive data types (also called Maps, and Associative
Arrays).
In each entry of the form the key is a string and the value must be a
primitive data type.
WHEN WE NEED THEM:
PREFERENCES are typically used to keep state information and shared
data among several activities of an application.
Shared Preferences is the storage, in android, that you can use to
store some basic things related to functionality, users' customization
or its profile.
Suppose you want to save user's name in your app for future purposes.
You cant save such a little thing in database, So you better keep it
saved in your Preferences. Preferences is just like a file , from
which you can retrieve value anytime in application's lifetime in a
KEY-VALUE pair manner.
Take another example, If you use whatsapp, we have a wallpaper option
there. How the application knows which image serves as wall-paper for
you whenever you open your whatsapp. This information is stored in
preferences. Whenever you clear data for any app, preferences are
deleted
You are using Preferences and trying to write a value in Shared Preference so I just changed your code to using the Shared Preference.
For saving preferences
public static void setCountReceived(Context ctx, Integer countReceived)
{
SharedPreferences.Editor editor = getSharedPreferences(YOUR_PREF_NAME,MODE_PRIVATE).edit();
editor.putInt(PREF_COUNT_RECEIVED, countReceived);
editor.commit();
}
For retrieving preferences,
static SharedPreferences getSharedPreferences(Context ctx) {
return context.getSharedPreferences(YOUR_PREF_NAME,MODE_PRIVATE);
}

SharedPreferences always get default value in my existing app but when created new app its ok

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.

Best Practices for SharedPreferences

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.

SharedPreferences commit and rollback

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

android default values for shared preferences

I am trying to understand the SharedPreferences of Android. I am a beginner
and don't know a lot about it.
I have this class I implemented for my app Preferences
public class Preferences {
public static final String MY_PREF = "MyPreferences";
private SharedPreferences sharedPreferences;
private Editor editor;
public Preferences(Context context) {
this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
this.editor = this.sharedPreferences.edit();
}
public void set(String key, String value) {
this.editor.putString(key, value);
this.editor.commit();
}
public String get(String key) {
return this.sharedPreferences.getString(key, null);
}
public void clear(String key) {
this.editor.remove(key);
this.editor.commit();
}
public void clear() {
this.editor.clear();
this.editor.commit();
}
}
The thing is that I would like to set default preferences. They would be set when the app is installed and could be modified after by the application and stay persistent.
I heard about a preferences.xml but I don't understand the process.
Could someone help me?
Thanks for you time
Simple, if you want a separate default value for each variable, you need to do it for each one, but on your method:
public String get(String key) {
return this.sharedPreferences.getString(key,"this is your default value");
}
If the variable was never accessed by the user or was never created, the system will set the default value as value and if you or the user changed this value, the default value is ignored. See http://developer.android.com/guide/topics/data/data-storage.html#pref
Directly from the Android Documentation:
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).
Could you use the default value parameter of the getX() method?
For example, to get a String called 'username', you could use this:
String username = prefs.getString("username_key", "DefaultUsername");
You can simply define your default values in your Preferences class.
You can store default values in string resource:
<string name="key_name">default_value</string>
and then get it as it follows:
int ResId = context.getResources().getIdentifier(key_name, "string", context.getPackageName()));
prefs.getString(key_name,context.getResources().getString(ResId);

Categories

Resources