在Android中没有清除sharedpreferences

时间:2017-04-06 06:09:36

标签: android sharedpreferences android-sharedpreferences

当用户登录并在textview中设置时,我将数据存储在sharedpreferences上。我想在用户注销时删除一个特定数据。问题是数据正在存储但不能删除。我试过下面的代码。

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

6 个答案:

答案 0 :(得分:2)

删除特定值:SharedPreferences.Editor.remove()后跟提交()

要删除所有SharedPreferences.Editor.clear()后跟commit()

如果您不关心返回值并且从应用程序的主线程中使用它,请考虑使用apply()代替。

答案 1 :(得分:0)

试试这个,

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

答案 2 :(得分:0)

尝试实施此注销:

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

答案 3 :(得分:0)

您需要重新初始化共享首选项editer以清除数据

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

答案 4 :(得分:0)

SharedPreferences pref = context.getSharedPreferences("s_result", Context.MODE_PRIVATE);
pref.edit().clear().commit();

答案 5 :(得分:0)

首先,你需要创建像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;
    } 

您可以使用以下代码段

public void clearPreference() {

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