Android SharedPreference值在应用程序关闭时被清除



我正在SharedPreference上保存我的UserId以备将来使用。但如果我关闭该应用程序或终止该应用程序,这些数据就会被清除。

用于保存的代码:

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = app_preferences.edit();
editor.clear();
editor.putString("USERID", valu1);
editor.apply();

要检索的代码:

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
userID = app_preferences.getString("USERID", "");

有什么建议吗?

apply((方法是异步的,在后台线程上工作。它将数据缓存在RAM中,并等待,直到它有足够的资源将数据写入永久存储器。考虑到这一点,如果你立即关闭你的应用程序,你可能会丢失数据。与apply((不同,commit在UI线程上同步运行,它是否有保证的写入,但会暂停UI线程一段时间。不过,等待是微不足道的。考虑使用commit((而不是apply((,看看它是否有用。

为其创建一个会话管理器,这是一个分离的类。

public class SessionManager {

SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "Android_Session";
private static final String IS_LOGIN = "IsLoggedIn";
// User name, Email,Id (make variable public to access from outside)
public static final String KEY_PHONE = "phone";
public static final String KEY_EMAIL = "email";
public static final String KEY_ID = "id";
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session when user login first time call this.
*/
public void createLoginSession(String phone, String email, String id) {

editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_PHONE, phone);
editor.putString(KEY_EMAIL, email);
editor.putString(KEY_ID, id);
editor.commit();
}

/**
* Check user is login or not if not.
* This will used in dynamic app where session expired from server sideit will redirect to login page.  
*/
public void checkLogin() {

if (!this.isLoggedIn()) {
// user is not logged in then redirect to Login Activity
Intent i = new Intent(_context, Login.class);

// Add flags launch modes
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();

user.put(KEY_PHONE, pref.getString(KEY_PHONE, null));
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
user.put(KEY_ID, pref.getString(KEY_ID, null));
return user;
}
/**
* Clear session details
*/
public void logoutUser() {
editor.clear();
editor.commit();
// After logout redirect user to Login Activity
Intent i = new Intent(_context, Login.class);
// Add Flags (Launch Modes)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

_context.startActivity(i);
}
/**
* Check if user login or not
**/
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}

现在检索等值

SessionManager session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
String id = user.get(SessionManager.KEY_ID); 

识别登录

SessionManager session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {                             
//ToDo
} else {
}

最新更新