简体   繁体   中英

sharedpreferences is not being cleared in android

I'm storing data on sharedpreferences when user is logged in and setting it in a textview. I want to remove one specific data when user logged out. The problem is data is being stored but not removing. I have tried below code.

public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;

private static final String PREF_NAME = "NaafcoPref";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_ID = "id";
public static final String KEY_RESULT = "result";
public static final String SCAN_RESULT = "s_result";

public SessionManager(Context context) {
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public void createLoginSession(String id) {
    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_ID, id);
    editor.commit();
}

public void getResult(String result) {
    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_RESULT, result);
    editor.commit();
}

public void getScanResult(String scanResult) {
    editor.putString(SCAN_RESULT, scanResult);
    editor.commit();
}

public void checkLogin() {
    if (!this.isLoggedIn()) {
        Intent i = new Intent(_context, PointActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(i);
    }
}


public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_ID, pref.getString(KEY_ID, null));
    user.put(KEY_RESULT, pref.getString(KEY_RESULT, null));
    user.put(SCAN_RESULT, pref.getString(SCAN_RESULT, null));
    return user;
}


public void logoutUser() {

    editor.remove(SCAN_RESULT).clear().commit();
    //editor.clear();
    //editor.commit();
    Intent i = new Intent(_context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    _context.startActivity(i);
}

public boolean isLoggedIn() {
    return pref.getBoolean(IS_LOGIN, false);
}
}

To remove specific values: SharedPreferences.Editor.remove() followed by a commit()

To remove them all SharedPreferences.Editor.clear() followed by a commit()

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

Try this,

 public void logoutUser() {


   SharedPreferences sp = Preferences.getInstance().pref;
            SharedPreferences.Editor editor = sp.edit();
            editor.remove(SCAN_RESULT);

            editor.commit();

        Intent i = new Intent(_context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(i);
    }

Try implementing this for logout:

   /**
     * Clear All user preferences
     * Use this when user logs out
     */
    public void clearPreferences() {
        isLoggedIn(); // this will set the login as false
        pref.edit().clear().apply();
        //also clear from default preferences
        SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(context);
        defaultPref.edit().clear().apply();
    }

You need to reinitialize sharepreference editer to clear data

 public void logoutUser() {
    editor = pref.edit();
    editor.clear().commit();
    Intent i = new Intent(_context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    _context.startActivity(i);
}
SharedPreferences pref = context.getSharedPreferences("s_result", Context.MODE_PRIVATE);
pref.edit().clear().commit();

First you need to create singleton class like PreferenceManager

private PreferenceManager(Context ctx) {
        prefs = ctx.getApplicationContext().getSharedPreferences(ctx.getPackageName(), Context.MODE_PRIVATE);
        editor = prefs.edit();
    }

    public static PreferenceManager getInstance(Context ctx) {
        if (sInstance == null) {
            sInstance = new PreferenceManager(ctx);
        }
        return sInstance;
    } 

You can use below snippet

public void clearPreference() {

        editor.remove("your preference key that you want to clear");
        .
        .add all preference key that you want to clear
        .
        editor.commit();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM