Reputation: 1632
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
Upvotes: 5
Views: 2923
Reputation: 773
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 !!
Upvotes: 0
Reputation: 5705
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");
Upvotes: 0
Reputation: 75788
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.
Upvotes: 2
Reputation: 369
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.
Upvotes: 0