Mike Ramos Mike Ramos - 4 months ago 18
Android Question

Why Static Shared Preferences are not saved?

I want to access Shared preferences static way to avoid using excessive code, but when I read shared preference, looks like was not saved whith the static method "setSyncDBIsNeeded()", what I'm doing wrong?

MyApplication code:

public class MyApplication extends Application {
private static MyApplication instance;

@Override
public void onCreate() {
super.onCreate();
instance = this;
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(0)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);

}

public static Context getContext() {
return instance.getApplicationContext();
}
}


My preferences activity:

public class PreferenceController {
SharedPreferences sharedPreferences;
private static String project = "com.example.myproject";

public PreferenceController() {
sharedPreferences = MyApplication.getContext().getSharedPreferences(project, Context.MODE_PRIVATE);
}

public PreferenceController(Context context) {
sharedPreferences = context.getSharedPreferences(project, Context.MODE_PRIVATE);
}

/* getters and setters */

// Static methods

public static void setSyncDBIsNeeded(boolean value) {
Log.d("PREFCON","Setted DBSyncNeeded : "+value);
getSharedPrefferences().edit().putBoolean("DBSyncNeeded", value);
}

public static boolean getSyncDBIsNeeded() {
Log.d("PREFCON","DBSyncNeeded: "+getSharedPrefferences().getBoolean("DBSyncNeeded", false));
return getSharedPrefferences().getBoolean("DBSyncNeeded", false);
}

private static SharedPreferences getSharedPrefferences() {
return MyApplication.getContext().getSharedPreferences(project, Context.MODE_PRIVATE);
}
}


Then in another class I do:

PreferenceController.setSyncDBIsNeeded(true);
PreferenceController.getSyncDBIsNeeded();


and its printed in Log:

07-14 14:24:04.665 27658-27658/com.example.myproject D/PREFCON: Setted DBSyncNeeded : true
07-14 14:24:04.665 27658-27658/com.example.myproject D/PREFCON: DBSyncNeeded: false

Answer

Try this:

SharedPreferences.Editor editor = getSharedPrefferences().edit();
editor.putBoolean("DBSyncNeeded", value);
editor.commit();

You have to remember to update the changes made to the SharedPreferences, so SharedPreferences actually save it.

Inserted into your code:

public static void setSyncDBIsNeeded(boolean value) {
    Log.d("PREFCON","Setted DBSyncNeeded : "+value);
    SharedPreferences.Editor editor = getSharedPrefferences().edit();
    editor.putBoolean("DBSyncNeeded", value);
    boolean completed = editor.commit();
    Log.e("PREFCON", "Updating SharedPreferences was " + completed + "!";
}

By adding a boolean value to be set to editor.commit you can easily know if it was a success or not. According to the documentation the commit() method returns a boolean value based on if it completed or not. True means the editing was successfull, while false means something went wrong.