user1139880
user1139880

Reputation: 1838

How to read/write a boolean preference

Edit: this code actually works. I had problem in the code that used it. Leaving it anyway in case anybody will find it useful.

I have a class with two methods to write and read a boolean persisted preference. However, if I write a new value and then try to read it, I still get the old value. Only if I kill the app and relaunch it, I do get the new value. Any idea what the problem is?

 Context mContext;

 ....
 public void writeFlag(boolean flag) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean("mykey", flag);
        editor.commit();
}

public boolean readFlag() {
        SharedPreferences sharedPreferences =
               PreferenceManager.getDefaultSharedPreferences(mContext);
        return sharedPreferences.getBoolean("mykey", false);
}

Upvotes: 6

Views: 9790

Answers (3)

Bosco Sabin
Bosco Sabin

Reputation: 124

public class SharePref {
private static final String TAG = SharePref.class.getSimpleName();
private static SharePref mThis;
private Context mContext;
private SharedPreferences mPreference;

private SharePref() {

}

public static void init(Context context) {
    if (mThis == null) {
        mThis = new SharePref();
        mThis.setData(context);
    }
}

private void setData(Context context) {
    mThis.mContext = context;
    mPreference = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
}

public static SharePref getInstance() {
    return mThis;
}

public void writeString(String tag, String data) {
    SharedPreferences.Editor editor = mPreference.edit();
    editor.putString(tag, data).apply();
}

public String readString(String tag) {
    if (mPreference == null)
        mPreference = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
    return mPreference.getString(tag, null);
}

public void writeBoolean(String tag, boolean data) {
    SharedPreferences.Editor editor = mPreference.edit();
    editor.putBoolean(tag, data).apply();
}

public boolean readBoolean(String tag) {
    return mPreference.getBoolean(tag, false);
}


public void clear(String tag) {
    mPreference.edit().remove(tag).apply();
}


}                                                                              

//In Activity

 SharePref.init(this);       
 SharePref.getInstance().writeString("key","value");                                                                                    

Upvotes: 0

Ahmad
Ahmad

Reputation: 201

public static boolean getBooleanFromSP(String key) {
// TODO Auto-generated method stub
SharedPreferences preferences = 
getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", 
  android.content.Context.MODE_PRIVATE);
 return preferences.getBoolean(key, false);
  }//getPWDFromSP()


 public static void saveBooleanInSP(String key, boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor =      preferences.edit();
editor.putBoolean(key, value);
editor.commit();
 }//savePWDInSP()

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

public static boolean getBooleanFromSP(String key) {
// TODO Auto-generated method stub
    SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
    return preferences.getBoolean(key, false);
}//getPWDFromSP()


public static void saveBooleanInSP(String key, boolean value){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor =      preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}//savePWDInSP()

Upvotes: 5

Related Questions