-1

I have situation in my app. Let's take two activities A & B . A is the main activity(which gets started when app is opened). B is started from A. I have a check box in B, who's boolean value using isChecked() method is assigned to a boolean called value & is saved to SharedPreferences. The code for saving to SharedPreferences is

SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("value", value);
editor.commit();

Now I want to read this value from activity A(i.e the Main activity), during opening the app and depending on this value I need to perform additional operations.

The code I use to retrieve the value is

SharedPreferences sharedPreference=PreferenceManager.getDefaultSharedPreferences(this);
boolean value=sharedPreferences.getBoolean("value", false);

The problem is that I am not able to retrive this values in activity A.Since the value is not retrieved the operations depending on value are not carried out.Also no matter how many times I check the checkBox upon restarting the application CheckBox remain unchecked in activity B. What is the problem?

Vivek Sasidharan
  • 1,265
  • 3
  • 17
  • 34

1 Answers1

2

You can create singleton class that manage app's preferences (do not forget make your own YourAppSingleton class):

public final class PreferencesUtil {

    private PreferencesUtil() {}

    public static float getFloatValue(String key, float defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getFloat(key, defaultValue);
    }

    public static void setFloatValue(String key, float value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public static long getLongValue(String key, long defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getLong(key, defaultValue);
    }

    public static void setLongValue(String key, long value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public static void setIntValue(String key, int value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }


    public static int getIntValue(String key, int defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getInt(key, defaultValue);
    }

    public static void setStringValue(String key, String value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getStringValue(String key) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getString(key, null);
    }

    public static String getStringValue(String key, String defValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getString(key, defValue);
    }

    public static boolean getBooleanValue(String key, boolean defaultValue) {
        SharedPreferences preferences = getSharedPreferences();
        return preferences.getBoolean(key, defaultValue);
    }

    public static void setBooleanValue(String key, boolean value) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void clearValue(String key) {
        SharedPreferences preferences = getSharedPreferences();
        Editor editor = preferences.edit();
        editor.remove(key);
        editor.commit();
    }

    private static SharedPreferences getSharedPreferences() {
        return YourAppSingleton.getInstance().getSharedPreferences(YourAppSingleton.getInstance().getString(R.string.app_name), Context.MODE_PRIVATE);
    }
}

Application class:

public class YourAppSingleton extends Application {

    private static YourAppSingleton instance = null;

    public void onCreate() { 
        super.onCreate();
        instance = this;
    }

    public static YourAppSingleton getInstance() {
        return instance;
    }
}
DrMartens
  • 441
  • 5
  • 11
  • Didn't worked out , I get fc, due to null pointer exception, attempt to invoke virtual method MyAppSingleton.getString(int) on a null object. I am not that in to Singleton its new to me – Vivek Sasidharan Mar 30 '15 at 14:56
  • Okie, I fixed the fc, forgot to add singleton to manifest. But still the sharedpreference is not working, it simply returns the default value I am giving. – Vivek Sasidharan Mar 30 '15 at 15:31