Iam trying to make a checker and I want to save a value into SharedPreferences. But i'am not sure if it works
This what I do to save the value is : *
SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
boolean firstrun = prefs.getBoolean("firstrun", true);
db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory
if (firstrun) {
SharedPreferences.Editor editor = prefs.edit();
db.execSQL("CREATE TABLE startValue (ID Integer Primary Key, myValue Integer)");
db.execSQL("INSERT INTO startValue (myValue) VALUES (2)");
editor.putBoolean("firstrun", false);
editor.apply();
}
// Save the state
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
And to Clear the preferenece from another activity is :
try{
db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory
db.execSQL("DROP TABLE IF EXISTS startValue");
db.close();
SharedPreferences preferences = getPreferences(0);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("firstrun");
editor.clear();
editor.commit();
this.finish();
}
catch(SQLException ex)
{
//catch error here
}
Issue
But when i'am testing as I see its not clearing the preferences. Am I doing something wrong or?
You use remove() to remove specific preferences, you use clear() to remove them all.
Clearing shared preferences To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes.
To clear SharedPreferences use this
SharedPreferences preferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
Hope this helped you.
You are not using the same preferences. Take a while to read http://developer.android.com/reference/android/app/Activity.html
In your first activity you are using:
SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
And in the other activity clearing you are only using:
SharedPreferences preferences = getPreferences(0);
Reading the docs:
Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
You need to use the same preference name in both activities. So in your second activity, where you do the clearing just use
SharedPreferences preferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
// save string to SharedPreferences
public static void saveStringToSharedPreferences(Context mContext, String key, String value) {
if(mContext != null) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
if(mSharedPreferences != null)
mSharedPreferences.edit().putString(key, value).commit();
}
}
// read string from SharedPreferences
public static String readStringFromSharedPreferences(Context mContext, String key) {
if(mContext != null) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
if(mSharedPreferences != null)
return mSharedPreferences.getString(key, null);
}
return null;
}
// remove from SharedPreferences
public static void removeFromSharedPreferences(Context mContext, String key) {
if (mContext != null) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0);
if (mSharedPreferences != null)
mSharedPreferences.edit().remove(key).commit();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With