Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences Clear/Save

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?

like image 784
Tirolel Avatar asked Mar 18 '13 11:03

Tirolel


People also ask

What is the syntax for deleting data in shared preferences?

You use remove() to remove specific preferences, you use clear() to remove them all.

How do I delete shared preferences in Kotlin?

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.


3 Answers

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.

like image 147
AwadKab Avatar answered Oct 20 '22 00:10

AwadKab


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);
like image 22
David Olsson Avatar answered Oct 19 '22 22:10

David Olsson


// 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();
    }
}
like image 22
Janoub Avatar answered Oct 19 '22 23:10

Janoub