4

I am new to android and learning. I have theme changing option in my application from where the user can switch themes. I was using a global variable for saving theme number in the app but it was getting a loss when application get cleared from the background. So I have thought for use SharedPreferences for this purpose. I have found one simple and easy way to store and retrieve SharedPreference from here.

My code is like below :

public class Keystore {
    private static Keystore store;
    private SharedPreferences SP;
    private static String filename="Keys";
    public static int theme=1;

    private Keystore(Context context) {
        SP = context.getApplicationContext().getSharedPreferences(filename,0);
    }

    public static Keystore getInstance(Context context) {
        if (store == null) {
            store = new Keystore(context);
        }
        return store;
    }

    public void put(String key, String value) {
        SharedPreferences.Editor editor;
        editor = SP.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public String get(String key) {
        return SP.getString(key, null);
    }

    public int getInt(String key) {
        return SP.getInt(key, 0);
    }

    public void putInt(String key, int num) {
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.putInt(key, num);
        editor.apply();
    }

    public void clear(){
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.clear();
        editor.apply();
    }

    public void remove(){
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.remove(filename);
        editor.apply();
    }
}

And as per example given in original answer, I am trying to use it in my activity class like below for getting value

int theme= store.getInt("theme");
Log.d(getClass().getName(),"theme"+theme);

But it's returning 0 instead of 1. I have also doubt that I have saved default value as 1 in that class like public static int theme=1; This is the correct way for saving default value in SharedPreferences?

Thanks

Dinal Koyani
  • 461
  • 3
  • 6
Priya
  • 1,402
  • 4
  • 14
  • 35

4 Answers4

2

You should use commit ()

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

public void putInt(String key, int num) 
    {
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.remove("key");
        editor.putString("key", num);
        editor.commit(); // IF commit() showing warning then use apply() instead .
        editor.apply();

    }

NOTE

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
0

If you want to fetch by default 1 value when you when you don't save data in sharepreferance then you have to change in in your method like

public int getInt(String key) {
    return SP.getInt(key, 1);
}

It will work for you.

Akshay More
  • 271
  • 2
  • 12
  • Hi ! Thanks for nice suggestion. I have one more question. In Example class its used SP = context.getApplicationContext().getSharedPreferences(filename,0); like this. What if I want multiple key to store – Priya Oct 16 '17 at 05:49
  • For take more information about sharepreferance please see link : https://www.journaldev.com/9412/android-shared-preferences-example-tutorial – Akshay More Oct 16 '17 at 05:56
  • If you like my answer then please accept it and upvote it. :) – Akshay More Oct 16 '17 at 05:58
0

You have to first save value in shared preferences so that you can retrieve it later. To save it use the below code

store.putInt(your int value);

and retrieve it from shared preference same like you are doing

int theme= store.getInt("theme");
Vivek Mishra
  • 5,269
  • 7
  • 41
  • 74
0

You can do it this way :

private void saveTheme()
{
SharedPreferences sharedpreferences = getSharedPreferences("filename here", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("theme", 1);
editor.commit();
} 



private int getTheme()
{
SharedPreferences sharedpreferences = getSharedPreferences("filename here", Context.MODE_PRIVATE);
sharedpreferences.getInt("theme",0);
}

You can add them in Utility class or make a seperate PreferenceHelper class and make sharedPreference global.

Hope it Helps !!

Animesh Mangla
  • 763
  • 7
  • 31