I use Preference Manager to save some Integers and boolean values. I have created a SettingsPreferences class :
public class SettingsPreferences {
private Context mContext;
public SettingsPreferences(Context context) {
this.mContext = context;
}
public boolean isNull() {
if (PreferenceManager.getDefaultSharedPreferences(mContext) == null) {
return true;
} else {
return false;
}
}
public void setBoolean(String name, boolean value) {
PreferenceManager.getDefaultSharedPreferences(mContext).edit()
.putBoolean(name, value).apply();
}
public void setInt(String name, int value) {
PreferenceManager.getDefaultSharedPreferences(mContext).edit()
.putInt(name, value).apply();
}
public boolean getBoolean(String name, boolean defaultValue) {
return PreferenceManager.getDefaultSharedPreferences(mContext)
.getBoolean(name, defaultValue);
}
public int getInt(String name, int defaultValue) {
return PreferenceManager.getDefaultSharedPreferences(mContext).getInt(
name, defaultValue);
}}
In the onCreate method in the Main class I add the default values :
mSettingsPreferences = new SettingsPreferences(getApplicationContext());
if(mSettingsPreferences.isNull() == true) {
mSettingsPreferences.setBoolean("MAX", 1);
mSettingsPreferences.setBoolean("PROGRESS", 1);
}
In a fragment class I need to load that data and displayed it in a progress bar. Here is the code :
Thread t = new Thread() {
public void run() {
try {
sleep(100);
SettingsPreferences sett = new SettingsPreferences(mContext);
mProgBar.setMax(sett.getInt("MAX", 2));
mProgBar.setProgress(sett.getInt("PROGRESS", 1));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
The context is defined I checked him, the progressbar also, but every time it loads me the default values. What is the problem?