我正在为登录用户存储身份验证令牌,SharedPreferences
并在注销时清除首选项。
private void doSignOut(){
SharedPreferences pref= getSharedPreferences(Constants.SHARED_PREF_DIR, Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.clear();
editor.apply();
boolean isCleared = editor.commit();
FLog.d("Is Pref cleared = " + isCleared);
mDbHelper.onUpgrade(mDbHelper.getWritableDatabase(), 0, 0);
mDbHelper.close();
Intent intent = new Intent(this, SignInActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
然后在我的SignInActivity
我检查authToken
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
checkForSignIn();
}
private void checkForSignIn() {
SharedPreferences pref = getSharedPreferences(Constants.SHARED_PREF_DIR, Context.MODE_PRIVATE);
String authToken = pref.getString(Constants.SHARED_PREF_AUTH, null);
if(null == authToken){
showAnimations();
} else {
FLog.d("AuthToken already present " + authToken);
Intent intent = new Intent(getApplicationContext(), com.ribbon.ribbon.MainNavigationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
问题是在checkForSignIn()
authToken 中null
不是保存实际值。
我在退出之前检查了首选项 xml,它包含值,而在退出后它被清除。
这里有什么问题?我错过了一些非常明显的东西吗?请帮忙。