-1

This is in my main activity class, I can pass and retrieve data in my other class through shared preferences but I cant clear the shared preferences in my other class. Also tell me how to check that my shared preferences are cleared.

SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "userKey";
public static final String Pass = "passKey";

 sharedPreferences=getApplicationContext().getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);

                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(Name,userName);
                    editor.putString(Pass,password);
                    editor.commit();

this is in my other class

SharedPreferences sharedPreferences;
    sharedPreferences=getApplicationContext().getSharedPreferences(SignUPActivity.MyPREFERENCES, Context.MODE_PRIVATE);

 SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.apply();
            editor.commit();
Rahul
  • 10,457
  • 4
  • 35
  • 55

2 Answers2

0

Possible Reason is you are not using the same preferences.

In your first activity you are using:

sharedPreferences = getApplicationContext()
                    .getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);

And in the other activity you should also use it.

Try to read these http://developer.android.com/reference/android/app/Activity.html

also from 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.

cske
  • 2,233
  • 4
  • 26
  • 24
Odysseus
  • 1
  • 1
0

Hi if you want just pass value from one Activity to Another or One Fragment to other than I suggest you to use INTENT to pass data

Look at this example how to pass -http://startandroid.ru/en/lessons/241-lesson-28-extras-passing-data-using-intent.html

Also -https://stackoverflow.com/a/30166602/4741746

sharedPreferences is used when data used for all activity which is permanatly stored in application you also can clear data

Remove given String from shearedPrefrance

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();
    }
}

Remove All value from SharedPrefrance

public static void removeSharedPreferences(Context mContext) {
    if (mContext != null) {
       SharedPreferences preferences = mContext.getSharedPreferences("PREFERENCE", 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear(); 
        editor.commit();
    }
}

And why your code is not working is you have to passs Context.MODE_PRIVATE insted MODE_PRIVATE jsut confirm both should have same value

Put this code in both the classes

 SharedPreferences app_preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55