我将用户登录详细信息保存到我的SharedPreferences文件中。问题是当我尝试清除ShredPreferences时,细节不清楚。当下一个用户登录时,他的详细信息不会显示。
这是我的代码: -
Save.java
sessionManager.createLoginSession(username, deviceId, name );
SessionManagement.java
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "UserDetails";
public SessionManagement(Context context)
{
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String emailId, String deviceid,String usersname)
{
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
editor.putBoolean(KEY_DEVICEREGISTERED, true);
editor.putString(KEY_EMAILID, emailId);
editor.putString(KEY_DEVICEiD, deviceid);
editor.putString(KEY_USERSNAME, usersname);
// commit changes
editor.commit();
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails()
{
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_EMAILID, pref.getString(KEY_EMAILID, null));
user.put(KEY_DEVICEiD, pref.getString(KEY_DEVICEiD, null));
user.put(KEY_USERSNAME, pref.getString(KEY_USERSNAME, null));
// return user
return user;
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin()
{
// Check login status
if(!this.isLoggedIn())
{
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
// This function clears all session data and redirect the user to LoginActivity
/**
* Clear session details
* */
public void logoutUser()
{
// Clearing all data from Shared Preferences
//editor.clear();
editor.remove(KEY_DEVICEiD);
editor.remove(KEY_EMAILID);
editor.remove(KEY_USERSNAME);
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
public boolean isLoggedIn()
{
return pref.getBoolean(IS_LOGIN, false);
}
Logout.java
SessionManagement session = new SessionManagement(getApplicationContext());
session.logoutUser();
结束();
答案 0 :(得分:0)
我正在使用结构
public void logoutUser() {
// Clearing all data from Shared Preferences
editor.putBoolean(IS_LOGIN, false);
editor.putString(KEY_TOKEN, "");
editor.putString(KEY_EMAIL, "");
editor.commit();
}
答案 1 :(得分:0)
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all user data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
Try the above code.Hope it will work.