簡體   English   中英

在Android中未清除sharedpreferences

[英]sharedpreferences is not being cleared in android

當用戶登錄並將其設置在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);
}
}

刪除特定值: SharedPreferences.Editor.remove()后跟commit()

要刪除它們,所有SharedPreferences.Editor.clear()后跟commit()

如果您不關心返回值,並且正在應用程序的主線程中使用返回值,請考慮改用apply()

嘗試這個,

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

嘗試實現此功能以注銷:

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

您需要重新初始化sharepreference編輯器以清除數據

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

首先,您需要創建像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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM